首先,VC中添加#import <MobileCoreServices/MobileCoreServices.h> 使用(NSString *) kUTTypeImage定义在其中
判断是否授权:
NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudio AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
AVAuthorizationStatusNotDetermined = 0,//未明确
AVAuthorizationStatusRestricted,//受限
AVAuthorizationStatusDenied,//不允许
AVAuthorizationStatusAuthorized//允许
第一种使方法照相机:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];//添加代理 imagePicker.delegate = self;//是否允许编辑 imagePicker.allowsEditing = YES;//资源类型 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//media类型 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];//转跳 [self presentModalViewController:imagePicker animated:YES];
第二种使用摄像机:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//media类型 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];//视频品质 imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow; [self presentModalViewController:imagePicker animated:YES];
第三种使用相册取图片类型:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)kUTTypeImage, nil]; [self presentModalViewController:imagePicker animated:YES];
第四种使用相册取视频类型:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil]; [self presentModalViewController:imagePicker animated:YES];
代理:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) { //获取照片的原图 UIImage* original = [info objectForKey:UIImagePickerControllerOriginalImage]; //获取图片裁剪的图 UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage]; //获取图片裁剪后,剩下的图 UIImage* crop = [info objectForKey:UIImagePickerControllerCropRect]; //获取图片的url NSURL* url = [info objectForKey:UIImagePickerControllerMediaURL]; //获取图片的metadata数据信息 NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata]; //如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片 // UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) { NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; self.fileData = [NSData dataWithContentsOfFile:videoPath]; } [picker dismissModalViewControllerAnimated:YES]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissModalViewControllerAnimated:YES]; }
时间: 2024-10-12 13:37:54