ios 通过调用系统摄像头拍照,拍视频,然后写入文件

如果您的App里需要获得由系统自带的照相机、摄像机和录音软件所生成的文件。可以借鉴以下代码来调用iPhone摄像头拍照或者摄像的功能,并把获得的数据直接写入到文件。

//这一段是,点击一个takePicture按钮的操作.

01 - (IBAction)takePicture:(id)sender {
02   
03 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
04   
05 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
06 picker.sourceType = UIImagePickerControllerSourceTypeCamera;
07 NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
08 picker.mediaTypes = temp_MediaTypes;
09 picker.delegate = self;
10 picker.allowsImageEditing = YES;
11 }
12   
13 [self presentModalViewController:picker animated:YES];
14 [picker release];
15   
16 }

//下面两个函数是遵守 UIImagePickerControllerDelegate这个协议所实现的类.这样就能够完整的实现,获取照片或者视频,然后写入文件的过程.

01     - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
02     {
03       
04     NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
05       
06     BOOL success;
07     NSFileManager *fileManager = [NSFileManager defaultManager];
08     NSError *error;
09       
10     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
11     NSString *documentsDirectory = [paths objectAtIndex:0];
12       
13     if ([mediaType isEqualToString:@"public.image"]){
14       
15     UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
16 ;    NSLog(@“found an image”);
17       
18     NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:@"temp.jpg"];
19 ;    NSLog(@“%@”, ,imageFile);
20       
21     success = [fileManager fileExistsAtPath:imageFile];
22     if(success) {
23     success = [fileManager removeItemAtPath:imageFile error:>error];
24     }
25       
26     imageView.image = image;
27     [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];
28       
29     //SETIMAGE(image);
30     //CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
31 ;    }
32     else if([mediaType isEqualToString:@"public.movie"]){
33     NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
34     NSLog(@“%@”, ,videoURL);
35     NSLog(@“found a video”);
36     NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
37       
38     /****************************************/
39       
40     NSString *videoFile = [documentsDirectory stringByAppendingPathComponent:@"temp.mov"];
41 ;    NSLog(@“%@”, ,videoFile);
42       
43     success = [fileManager fileExistsAtPath:videoFile];
44     if(success) {
45     success = [fileManager removeItemAtPath:videoFile error:>error];
46     }
47     [videoData writeToFile:videoFile atomically:YES];
48     //CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
49 ;    //NSLog(videoURL);
50     }
51     [picker dismissModalViewControllerAnimated:YES];
52     }
53       
54     - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
55       
56     [picker dismissModalViewControllerAnimated:YES];
57       
58     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值