音视频开发iOS头条干货一些细节功能

iOS~一行代码实现拍照、录像和相册功能

2016-08-10  本文已影响1998人  爱上别的吧
�图片来自网络
前段时间工作中遇到了调用系统相机、相册需求,因此写了一个小工具,现在分享给大家。
1.概述

在iOS中要实现拍照、录像和相册最简单快捷的方法就是使用UIImagePickerController;本工具就是将UIImagePickerController做了进一步封装,使其更加便捷,让大家一行代码实现拍照、录像和相册功能。

2.接口方法
/**
 *  单例
 *
 *  @return VDCameraAndPhotoTool对象
 */
+ (instancetype)shareInstance;
/**
 *  调用系统相机录像
 *
 *  @param vc         要调用相机的控制器
 *  @param finishBack 录像完成的回调
 */
 - (void)showVideoInViewController:(UIViewController *)vc andFinishBack:(cameraReturn)finishBack;
/**
 *  调用系统相机
 *
 *  @param vc         要调用相机的控制器
 *  @param finishBack 拍照完成的回调
 */
- (void)showCameraInViewController:(UIViewController *)vc andFinishBack:(cameraReturn)finishBack;
/**
 *  调用系统相册
 *
 *  @param vc         要调用相册的控制器
 *  @param finishBack 选择完成的回调
 */
- (void)showPhotoInViewController:(UIViewController *)vc andFinishBack:(cameraReturn)finishBack;

/**
 *  显示相机、录像或相册(弹出alert)
 *
 *  @param vc        控制器
 *  @param finsished 完成回掉
 */
- (void)showImagePickerController:(UIViewController *)vc andFinishBack:(cameraReturn)finishBack;
3.使用演示
- (IBAction)cameraClick:(id)sender {

  [[VDCameraAndPhotoTool shareInstance] showCameraInViewController:self andFinishBack:^(UIImage *image,NSString *videoPath) {
    
     [self removePlayerLayer];
    
     if (image) {
        
        self.imgView.image = image;
     } 
  }];
}

- (IBAction)sheetClick:(id)sender {

    [[VDCameraAndPhotoTool shareInstance] showImagePickerController:self andFinishBack:^(UIImage *image,NSString *videoPath) {
    
      if (image) {//图片
        
          [self removePlayerLayer];
        
          self.imgView.image = image;
      }
    
      if (videoPath) {//视频
        
          self.imgView.image = nil;
        
          [self addPlayerLayer:videoPath];
      }
  }];
}

- (IBAction)photoClick:(id)sender {

    [[VDCameraAndPhotoTool shareInstance] showPhotoInViewController:self andFinishBack:^(UIImage *image,NSString *videoPath) {
    
        [self removePlayerLayer];
    
        if (image) {
        
          self.imgView.image = image;
        }
    
    }];
}

 - (IBAction)videoClick:(id)sender {

    [[VDCameraAndPhotoTool shareInstance] showVideoInViewController:self andFinishBack:^(UIImage *image,NSString *videoPath) {
    
       if (videoPath) {
        
          self.imgView.image = nil;
        
          [self addPlayerLayer:videoPath];
       }
    
     }];
}

/**
 *  移除player
 */
- (void)removePlayerLayer {

    for (CALayer *layer in self.imgView.layer.sublayers) {
    
        if ([layer isKindOfClass:[AVPlayerLayer class]]) {
        
             [layer removeFromSuperlayer];
             self.player = nil;
        }
    }
}
/**
 *  添加player
 */
- (void)addPlayerLayer:(NSString *)videoPath {

    NSURL *url = [NSURL fileURLWithPath:videoPath];
    _player = [AVPlayer playerWithURL:url];
    AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:_player];
    playerLayer.frame = self.imgView.frame;
    [self.imgView.layer addSublayer:playerLayer];
    [_player play];
}
4.参考资料

如果对iOS多媒体方面不太熟悉的同学可以查阅以下资料以及系统文档:
http://www.cnblogs.com/kenshincui/p/4186022.html#camera
http://blog.csdn.net/chenyong05314/article/details/17357221

如果想深度自定义相机的可以查看这篇文章:
http://www.jianshu.com/p/5860087c8981

5.源码

源码放在gitHub上,欢迎指正,记得star哦!

上一篇下一篇

猜你喜欢

热点阅读