使用UIImagePickerController拍照和视频录制

原创 2015年07月10日 15:54:18

一 UIImagePickerController简介和属性介绍。

UIImagePickerController继承于UINavigationController。UIImagePickerController可以用来选择照片,它还可以用来拍照和录制视频。首先看一下这个类常用的属性和方法:

属性 说明
@property(nonatomic)           UIImagePickerControllerSourceType     sourceType 拾取源类型,sourceType是枚举类型:
UIImagePickerControllerSourceTypePhotoLibrary:照片库
,默认值
UIImagePickerControllerSourceTypeCamera:摄像头
UIImagePickerControllerSourceTypeSavedPhotosAlbum:相簿
@property(nonatomic,copy)      NSArray                              *mediaTypes 媒体类型,默认情况下此数组包含kUTTypeImage,所以拍照时可以不用设置;但是当要录像的时候必须设置,可以设置为kUTTypeVideo(视频,但不带声音)或者kUTTypeMovie(视频并带有声音)
@property(nonatomic)           NSTimeInterval                        videoMaximumDuration 视频最大录制时长,默认为10 s
@property(nonatomic)           UIImagePickerControllerQualityType    videoQuality 视频质量,枚举类型:
UIImagePickerControllerQualityTypeHigh:高清质量
UIImagePickerControllerQualityTypeMedium:中等质量,适合WiFi传输
UIImagePickerControllerQualityTypeLow:低质量,适合蜂窝网传输
UIImagePickerControllerQualityType640x480:640*480
UIImagePickerControllerQualityTypeIFrame1280x720:1280*720
UIImagePickerControllerQualityTypeIFrame960x540:960*540
@property(nonatomic)           BOOL                                  showsCameraControls 是否显示摄像头控制面板,默认为YES
@property(nonatomic,retain)    UIView                                *cameraOverlayView  摄像头上覆盖的视图,可用通过这个视频来自定义拍照或录像界面
@property(nonatomic)           CGAffineTransform                     cameraViewTransform 摄像头形变
@property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode 摄像头捕获模式,捕获模式是枚举类型:
UIImagePickerControllerCameraCaptureModePhoto:拍照模式
UIImagePickerControllerCameraCaptureModeVideo:视频录制模式
@property(nonatomic) UIImagePickerControllerCameraDevice      cameraDevice 摄像头设备,cameraDevice是枚举类型:
UIImagePickerControllerCameraDeviceRear:前置摄像头
UIImagePickerControllerCameraDeviceFront:后置摄像头
@property(nonatomic) UIImagePickerControllerCameraFlashMode   cameraFlashMode  闪光灯模式,枚举类型:
UIImagePickerControllerCameraFlashModeOff:关闭闪光灯
UIImagePickerControllerCameraFlashModeAuto:闪光灯自动
UIImagePickerControllerCameraFlashModeOn:打开闪光灯
类方法 说明
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType 指定的源类型是否可用,sourceType是枚举类型:
UIImagePickerControllerSourceTypePhotoLibrary:照片库
UIImagePickerControllerSourceTypeCamera:摄像头
UIImagePickerControllerSourceTypeSavedPhotosAlbum:相簿
+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType 指定的源设备上可用的媒体类型,一般就是图片和视频
+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice 指定的摄像头是否可用,cameraDevice是枚举类型:
UIImagePickerControllerCameraDeviceRear:前置摄像头
UIImagePickerControllerCameraDeviceFront:后置摄像头
+ (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice 指定摄像头的闪光灯是否可用
+ (NSArray *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice 获得指定摄像头上的可用捕获模式,捕获模式是枚举类型:
UIImagePickerControllerCameraCaptureModePhoto:拍照模式
UIImagePickerControllerCameraCaptureModeVideo:视频录制模式
对象方法 说明
- (void)takePicture  编程方式拍照
- (BOOL)startVideoCapture  编程方式录制视频
- (void)stopVideoCapture 编程方式停止录制视频
代理方法 说明
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 媒体拾取完成
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 取消拾取
扩展方法(主要用于保存照片、视频到相簿) 说明
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo) 保存照片到相簿
UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath) 能否将视频保存到相簿
void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void *contextInfo) 保存视频到相簿

二 使用UIImagePickerController拍照和视频录制

#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *mImageview;

@property (nonatomic,assign) BOOL isTakeVedio;

@property (nonatomic,strong) UIImagePickerController *imagePicker;
@property (nonatomic,strong) AVPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePic:(id)sender {
    _isTakeVedio = NO;
    [self showPicker];
}

- (IBAction)takeVedio:(id)sender {
    _isTakeVedio = YES;
    [self showPicker];
}



- (UIImagePickerController *)imagePicker
{
    if (_imagePicker == nil) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;    //设置来源为摄像头
        _imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear; //设置使用的摄像头为:后置摄像头
        if (self.isTakeVedio) {
            _imagePicker.mediaTypes = @[(NSString *)kUTTypeVideo];    //设置为视频模式-<span style="color: rgb(51, 51, 51); font-family: Georgia, 'Times New Roman', Times, sans-serif; font-size: 14px; line-height: 25px;">注意媒体类型定义在MobileCoreServices.framework中</span>
            _imagePicker.videoQuality = UIImagePickerControllerQualityTypeIFrame1280x720;   //设置视频质量
            _imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;  //设置摄像头模式为录制视频
        }
        else{
            _imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; //设置摄像头模式为拍照
        }
    }
    return _imagePicker;
}

- (void)showPicker
{
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

#pragma mark-
#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:(NSString *)kUTTypeImage]) {
        //图片保存和展示
        UIImage *image;
        if (picker.allowsEditing) {
            image = [info objectForKey:UIImagePickerControllerEditedImage]; //允许编辑,获取编辑过的图片
        }
        else{
            image = [info objectForKey:UIImagePickerControllerOriginalImage]; //不允许编辑,获取原图片
        }
        self.mImageview.image = image;
        UIImageWriteToSavedPhotosAlbum(image,nil,nil, nil);
    }
    else if([type isEqualToString:(NSString *)kUTTypeVideo]){
        //视频保存后 播放视频
        NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
        NSString *urlPath = [url path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlPath)) {
            UISaveVideoAtPathToSavedPhotosAlbum(urlPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        }
    }
    
}

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

#pragma mark-
#pragma mark - share method

//视频保存后的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription);
    }else{
        NSLog(@"视频保存成功.");
        //录制完之后自动播放
        NSURL *url=[NSURL fileURLWithPath:videoPath];
        _player=[AVPlayer playerWithURL:url];
        AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:_player];
        playerLayer.frame = self.mImageview.frame;
        [self.mImageview.layer addSublayer:playerLayer];
        [_player play];
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

视频录制,照片拍摄,保存等,UIImagePickerController

// //  ViewController.m //  PickUp // //  Created by yiral on 16/6/29. //  Copyright ...

IOS 关于系统源生UIImagePickerController录制视频时间设置

UIImagePickerController的UI很漂亮,不想用三方框架。查询了很多资料,发现闲话的人很多,根本不能解决问题,于是看了苹果基层发现UIImagePickerController有这么...

UIImagePickerController详解

转载自:http://blog.csdn.net/kingsley_cxz/article/details/9157093 1.UIImagePickerController的静态方法: ...

使用UIImagePickerController实现 iOS录像拍照

1.先看一下它包含的属性以及方法 UIImagePickerController属性 说明 @property(nonatomic)  UIImagePickerControllerSo...

UIImagePickerController使用

最近在做照相,研究下UIImagePickerController的使用方法。引用UIImagePickerController  需用到两个代理用UIImagePickerController选择、...

UIImagePickerController

http://lishuaishuai.iteye.com/blog/1050566 中期视讯 HD》ipad终于上线了,闲着无事,研究下UIImagePickerControl...
  • think12
  • think12
  • 2011年11月24日 09:34
  • 4947

视频录制,照片拍摄,保存等,UIImagePickerController

视频录制,照片拍摄,保存等,UIImagePickerController
  • yiral
  • yiral
  • 2016年06月29日 15:35
  • 695

iOS开发之UIImagePickerController详解

iOS开发之UIImagePickerController详解
  • l2i2j2
  • l2i2j2
  • 2016年04月13日 23:51
  • 2752

OC学习之道:UIImagePickerController的使用,使用UIImagePickerController调用本地摄像头,本地相册

// // RootViewController.m // InvokPhoto // // Copyright (c) 2015年 朝夕. All rights reserved. // ...

iOS摄像头和相册-UIImagePickerController-浅析

在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频。并且从相册中选取我们需要的图片或者视频。 关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用UIImagePickerController拍照和视频录制
举报原因:
原因补充:

(最多只允许输入30个字)