主要的类:UIImagePickerController
遵循协议:<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
导入框架:#import <AVFoundation/AVFoundation.h> #import <MobileCoreServices/MobileCoreServices.h>
拍摄照片:
#pragma mark ----照片拍摄
UIImagePickerController *takePhotoPicker = [self createTakePhotoPicker];
if (takePhotoPicker) {
takePhotoPicker.delegate = weakSelf;
[weakSelf.navigationController presentViewController:takePhotoPicker animated:YES
completion:nil];
}
- (UIImagePickerController *)createTakePhotoPicker {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusDenied) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"请在iPhone的“设置-隐私-相机”选项中,允许%@访问你的相机。", APP_NAME]
delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
[alert show];
return nil;
}
UIImagePickerController *takePhotoPicker = [[UIImagePickerController alloc] init];
takePhotoPicker.allowsEditing = NO;
takePhotoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
takePhotoPicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
return takePhotoPicker;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您的设备不支持拍照功能"
delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
[alert show];
return nil;
}
}
视频录制:
#pragma mark ----视频录制
UIImagePickerController *takeVideoPicker = [self createTakeVideoPicker];
takeVideoPicker.videoMaximumDuration = 30.0f;
if (takeVideoPicker) {
takeVideoPicker.delegate = weakSelf;
[weakSelf presentViewController:takeVideoPicker animated:YES completion:nil];
}
- (UIImagePickerController *)createTakeVideoPicker {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusDenied) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"请在iPhone的“设置-隐私-相机”选项中,允许%@访问你的相机。", APP_NAME] delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
[alert show];
return nil;
}
UIImagePickerController *takeVideoPicker = [[UIImagePickerController alloc] init];
takeVideoPicker.allowsEditing = YES;
takeVideoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
takeVideoPicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
takeVideoPicker.videoQuality = UIImagePickerControllerQualityTypeMedium;
return takeVideoPicker;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您的设备不支持摄像功能" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
[alert show];
return nil;
}
}
从手机相册选取视频:
#pragma mark ----从手机相册选择视频
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
if ([self canUserPickVideosFromPhotoLibrary]){
[mediaTypes addObject:( NSString *)kUTTypeMovie];
}
[imagePickerController setMediaTypes:mediaTypes];
[imagePickerController setDelegate:self];
self.imagePickerController = imagePickerController;
[self presentViewController:imagePickerController animated:YES completion:nil];
- (BOOL) canUserPickVideosFromPhotoLibrary{
return [self cameraSupportsMedia:( NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType{
__block BOOL result = NO;
if ([paramMediaType length]== 0){
NSLog(@"Media type is empty.");
return NO;
}
NSArray *availableMediaTypes =[UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
[availableMediaTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL*stop) {
NSString *mediaType = (NSString *)obj;
if ([mediaType isEqualToString:paramMediaType]){
result = YES;
*stop= YES;
}
}];
return result;
}
拍照/视频的代理:
#pragma mark ----视频页面/拍照页面/相册页面 完成后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *originalImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
}
else if([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
NSString *videoPathString = (NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path];
NSURL *videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
}
}
#pragma mark ----进入 视频页面/拍照页面/相册页面 点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}