使用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拍照和视频录制

转载自:http://blog.csdn.net/jueyi1127/article/details/46831875 一 UIImagePickerController简介和属性介绍。 ...
  • TT_HC
  • TT_HC
  • 2016-09-10 13:33
  • 404

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

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

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

整合Camera和MediaRecorder实现拍照和录像

年末最后一篇文章,明天就可以回家过年了.今天带来的效果如下图所示: 效果图看到的拍照和摄像其实是一个自定义的SurfaceView,在里面实现了拍照和散光灯切换,前后摄像头切换,录像的功能,...

基于VCamera,仿微信录制短视频

基于VCamera,Android仿微信录制短视频,如果喜欢请star,如果觉得有纰漏请提交issue,如果你有更好的点子可以提交pull request。 recoder4.gif 使用...

Android开发-拍照录像-AndroidStudio(二)VCamera拍摄小视频初体验

转载请注明出处:http://blog.csdn.net/iwanghang/我正在参加CSDN 2016博客之星评选,希望得到您的宝贵一票~http://blog.csdn.net/vote/can...

UIImagePickerController 的部分用法详解

概述 不能定制界面,不可派生子类。 使用步骤: 检查制定源是否可用. isSourceTypeAvailable:方法. 检查可用媒体(视频还是只能是图片) available...

IOS开发之相册拾取器UIImagePickerController

UIImagePickerController概述: UIImagePickerController 类是获取选择图片和视频的用户接口。我们可以用这个controller选择我们所需要的图片和视频。...

使用UIImagePickerController拍照和视频录制

UIImagePickerController继承于UINavigationController。UIImagePickerController可以用来选择照片,它还可以用来拍照和录制视频。 //视频...

iOS 拍照和视频录制(UIImagePickerController)

下面看一下在iOS如何拍照和录制视频。在iOS中要拍照和录制视频最简单的方法就是使用UIImagePickerController。UIImagePickerController继承于UINaviga...

UIImagePickerController 选照片 拍照 录视频

最近研究了一下UIImagePickerController,发现以前的写的项目中用UIImagePickerController做的换头像的功能存在很多的不规范之处。so,重新整理一下,不为别的只为...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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