通过调用系统摄像头拍照,拍视频,然后写入文件的
2010-06-08 16:50
656 查看
如果您的App里需要获得由系统自带的照相机、摄像机和录音软件所生成的文件。可以借鉴以下代码来调用iPhone摄像头拍照或者摄像的功能,并把获得的数据直接写入到文件。
//这一段是,点击一个takePicture按钮的操作.
- (IBAction)takePicture:(id)sender {
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];
}
//下面两个函数是遵守 UIImagePickerControllerDelegate这个协议所实现的类.这样就能够完整的实现,获取照片或者视频,然后写入文件的过程.
- (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];
}
欢迎进入CocoaChina论坛参与讨论,本帖地址 http://www.cocoachina.com/bbs/read.php?tid-18588.html
//这一段是,点击一个takePicture按钮的操作.
- (IBAction)takePicture:(id)sender {
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];
}
//下面两个函数是遵守 UIImagePickerControllerDelegate这个协议所实现的类.这样就能够完整的实现,获取照片或者视频,然后写入文件的过程.
- (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];
}
欢迎进入CocoaChina论坛参与讨论,本帖地址 http://www.cocoachina.com/bbs/read.php?tid-18588.html
相关文章推荐
- 通过调用系统摄像头拍照,拍视频,然后写入文件的代码范例
- 通过调用系统摄像头拍照,拍视频,然后写入文件的代码范例.
- 通过调用系统摄像头拍照,拍视频,然后写入文件
- ios 通过调用系统摄像头拍照,拍视频,然后写入文件
- ios 通过调用系统摄像头拍照,拍视频,然后写入文件
- 通过调用系统摄像头拍照,拍视频
- Android中通过Intent 调用系统的图片、视频、音频、录音、拍照
- [Jmeter]通过批处理调用java,java从CSV动态读取登录的用户名和密码,并将其作为参数组合成字符串,写入外部.bat文件,然后通过Java执行这个外部批处理文件
- Android中通过Intent 调用系统的图片、视频、音频、录音、拍照
- 主题:android调用系统摄像头拍照图片和视频
- 调用摄像头拍照或者摄像,把获得的数据写入到文件
- Android客户端之“微服私访”App的系统学习(八)调用系统摄像头拍照并管理照片并使用Okhttp上传文件至后台
- ios 通过调用系统摄像头拍照,…
- 在APP当中调用Android系统摄像头进行视频录制
- Android中通过Intent 调用图片、视频、音频、录音、拍照
- Java通过cmd调用FFmpeg实现大视频文件的分段切割
- 上传文件报错System.Net.ProtocolViolationException: 必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]GetResponse。
- FootStep 40、如何调用S60的系统摄像头功能进行拍照与摄像?
- 解决通过Intent调用系统拍照程序,返回图片太小的问题[android]
- Linux下通过系统调用(API)和C语言库函数对文件进行访问