分享

调用摄像头拍照或者摄像,把获得的数据写入到文件 | 小小iPhone开发

 ccccshq 2014-03-14

获取视频:

UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

委托代码:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:@"public.movie"])
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@”found a video”);
NSData *videoData = nil;
videoData = [NSData dataWithContentsOfURL:videoURL];
webData = [[NSMutableData alloc] init];
[webData appendData:videoData];
if (webData != nil) {
NSLog(@”SUCCESS!”);
}
}
}

调用摄像头拍照或者摄像,把获得的数据写入到文件.

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
picker.mediaTypes = temp_MediaTypes;
picker.delegate = self;
picker.allowsImageEditing = YES;
}

[self presentModalViewController:picker animated:YES];
[picker release];

//获取照片或者视频,然后写入文件
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

if ([mediaType isEqualToString:@"public.image"]){

UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
NSLog(@”found an image”);

NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:@"temp.jpg"];
NSLog(@”%@”, imageFile);

success = [fileManager fileExistsAtPath:imageFile];
if(success) {
success = [fileManager removeItemAtPath:imageFile error:&error];
}

imageView.image = image;
[UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];

//SETIMAGE(image);
//CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
}
else if([mediaType isEqualToString:@"public.movie"]){
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@”%@”, videoURL);
NSLog(@”found a video”);
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

NSString *videoFile = [documentsDirectory stringByAppendingPathComponent:@"temp.mov"];
NSLog(@”%@”, videoFile);

success = [fileManager fileExistsAtPath:videoFile];
if(success) {
success = [fileManager removeItemAtPath:videoFile error:&error];
}
[videoData writeToFile:videoFile atomically:YES];
//CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
//NSLog(videoURL);
}
[picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissModalViewControllerAnimated:YES];

}

本文固定链接: http://iphone./2012/11/20/%e8%b0%83%e7%94%a8%e6%91%84%e5%83%8f%e5%a4%b4%e6%8b%8d%e7%85%a7%e6%88%96%e8%80%85%e6%91%84%e5%83%8f%e6%8a%8a%e8%8e%b7%e5%be%97%e7%9a%84%e6%95%b0%e6%8d%ae%e5%86%99%e5%85%a5%e5%88%b0%e6%96%87%e4%bb%b6/ | 小小iPhone开发

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多