AVFoundation自定义相机

原文地址

AVFoundation自定义相机

一般需要使用相机时候, 调用系统的相机就可以了, 但是如果有复杂的自定义拍照需求的话, 使用更强大的AVFoundation就会很方便, 可以实现自定义拍照界面, 不显示预览层的盲拍, 以及不存储手机相册, 音量键拍照等功能都可以轻松实现.

导入依赖库 AVFoundation.framework

首先导入一个头文件

  1. #import <AVFoundation/AVFoundation.h>  
 #import <AVFoundation/AVFoundation.h>
由于后面我们需要将拍摄好的照片写入系统相册中,所以我们在这里还需要导入一个相册需要的头文件

  1. #import <AssetsLibrary/AssetsLibrary.h>  
  #import <AssetsLibrary/AssetsLibrary.h>

导入头文件后我们需要创建几个相机必须的属性

  1. /** 
  2.      *  AVCaptureSession对象来执行输入设备和输出设备之间的数据传递 
  3.      */  
  4.     @property (nonatomicstrong) AVCaptureSession* session;  
  5.       /** 
  6.        *  输入设备 
  7.        */  
  8.     @property (nonatomicstrong) AVCaptureDeviceInput* videoInput;  
  9.       /** 
  10.        *  照片输出流 
  11.        */  
  12.     @property (nonatomicstrong) AVCaptureStillImageOutput* stillImageOutput;  
  13.       /** 
  14.        *  预览图层 
  15.        */  
  16.     @property (nonatomicstrong) AVCaptureVideoPreviewLayer* previewLayer;  
/**
     *  AVCaptureSession对象来执行输入设备和输出设备之间的数据传递
     */
    @property (nonatomic, strong) AVCaptureSession* session;
      /**
       *  输入设备
       */
    @property (nonatomic, strong) AVCaptureDeviceInput* videoInput;
      /**
       *  照片输出流
       */
    @property (nonatomic, strong) AVCaptureStillImageOutput* stillImageOutput;
      /**
       *  预览图层
       */
    @property (nonatomic, strong) AVCaptureVideoPreviewLayer* previewLayer;

AVCaptureSession控制输入和输出设备之间的数据传递
AVCaptureDeviceInput调用所有的输入硬件。例如摄像头和麦克风
AVCaptureStillImageOutput用于输出图像
AVCaptureVideoPreviewLayer镜头捕捉到得预览图层

接下来初始化所有对象,下面这个方法的调用我放到viewDidLoad里面调用了

  1. - (void)initAVCaptureSession{  
  2.   
  3. self.session = [[AVCaptureSession alloc] init];  
  4.   
  5. NSError *error;  
  6.   
  7. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
  8.   
  9. //更改这个设置的时候必须先锁定设备,修改完后再解锁,否则崩溃  
  10. [device lockForConfiguration:nil];  
  11. //设置闪光灯为自动  
  12. [device setFlashMode:AVCaptureFlashModeAuto];  
  13.   
  14. //这句代码是对夜间拍照时候的自动补光, 如果没有这句代码, 晚上拍照基本上是黑色的, 比苹果系统的相机照片差很多  
  15. if (device.isLowLightBoostSupported) {  
  16.         device.automaticallyEnablesLowLightBoostWhenAvailable = YES;  
  17.     }  
  18.   
  19.  [device unlockForConfiguration];  
  20.   
  21. self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];  
  22. if (error) {  
  23.     NSLog(@”%@”,error);  
  24. }  
  25. self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];  
  26. //输出设置。AVVideoCodecJPEG   输出jpeg格式图片  
  27. NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil nil];  
  28. [self.stillImageOutput setOutputSettings:outputSettings];  
  29.   
  30. if ([self.session canAddInput:self.videoInput]) {  
  31.     [self.session addInput:self.videoInput];  
  32. }  
  33. if ([self.session canAddOutput:self.stillImageOutput]) {  
  34.     [self.session addOutput:self.stillImageOutput];  
  35. }  
  36.   
  37. //初始化预览图层  
  38. self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];  
  39. [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];  
  40. NSLog(@”%f”,kMainScreenWidth);  
  41. self.previewLayer.frame = CGRectMake(00,kMainScreenWidth, kMainScreenHeight - 64);  
  42. self.backView.layer.masksToBounds = YES;  
  43. [self.backView.layer addSublayer:self.previewLayer];  
  44.  }  
- (void)initAVCaptureSession{

self.session = [[AVCaptureSession alloc] init];

NSError *error;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

//更改这个设置的时候必须先锁定设备,修改完后再解锁,否则崩溃
[device lockForConfiguration:nil];
//设置闪光灯为自动
[device setFlashMode:AVCaptureFlashModeAuto];

//这句代码是对夜间拍照时候的自动补光, 如果没有这句代码, 晚上拍照基本上是黑色的, 比苹果系统的相机照片差很多
if (device.isLowLightBoostSupported) {
        device.automaticallyEnablesLowLightBoostWhenAvailable = YES;
    }

 [device unlockForConfiguration];

self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
if (error) {
    NSLog(@"%@",error);
}
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
//输出设置。AVVideoCodecJPEG   输出jpeg格式图片
NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
[self.stillImageOutput setOutputSettings:outputSettings];

if ([self.session canAddInput:self.videoInput]) {
    [self.session addInput:self.videoInput];
}
if ([self.session canAddOutput:self.stillImageOutput]) {
    [self.session addOutput:self.stillImageOutput];
}

//初始化预览图层
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
NSLog(@"%f",kMainScreenWidth);
self.previewLayer.frame = CGRectMake(0, 0,kMainScreenWidth, kMainScreenHeight - 64);
self.backView.layer.masksToBounds = YES;
[self.backView.layer addSublayer:self.previewLayer];
 }

之后在viewWillAppear,viewDidDisappear方法里开启和关闭session

注意:session的开启时很消耗CUP的  一直处于开启状态  会导致手机发热  不建议放在viewWillApper方法里面开启和viewDidDisappear里面关闭

最好是拍照前开启 拍照后关闭

  1. - (void)viewWillAppear:(BOOL)animated{  
  2.   
  3.     [super viewWillAppear:YES];  
  4.   
  5.     if ([self.session isRunning]) {  
  6.           
  7.     }else{  
  8.         [self.session startRunning];  
  9.     }  
  10. }  
  11.   
  12.   
  13. - (void)viewDidDisappear:(BOOL)animated{  
  14.   
  15.    [super viewDidDisappear:YES];  
  16.   
  17.    if ([self.session isRunning]) {  
  18.         [self.session stopRunning];  
  19.   
  20.     }  
  21. }  
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    if ([self.session isRunning]) {
        
    }else{
        [self.session startRunning];
    }
}


- (void)viewDidDisappear:(BOOL)animated{

   [super viewDidDisappear:YES];

   if ([self.session isRunning]) {
        [self.session stopRunning];

    }
}

到这里所有的初始化工作基本完成,运行程序可以看到镜头捕捉到得画面。接下来实现拍照按钮

接下来搞一个获取设备方向的方法,再配置图片输出的时候需要使用

  1.  -(AVCaptureVideoOrientation)avOrientationForDeviceOrientation:(UIDeviceOrientation)deviceOrientation  
  2.  {  
  3.     AVCaptureVideoOrientation result = (AVCaptureVideoOrientation)deviceOrientation;  
  4.     if ( deviceOrientation == UIDeviceOrientationLandscapeLeft )  
  5.        result = AVCaptureVideoOrientationLandscapeRight;  
  6.     else if ( deviceOrientation == UIDeviceOrientationLandscapeRight )  
  7.        result = AVCaptureVideoOrientationLandscapeLeft;  
  8.     return result;  
  9. }  
 -(AVCaptureVideoOrientation)avOrientationForDeviceOrientation:(UIDeviceOrientation)deviceOrientation
 {
    AVCaptureVideoOrientation result = (AVCaptureVideoOrientation)deviceOrientation;
    if ( deviceOrientation == UIDeviceOrientationLandscapeLeft )
       result = AVCaptureVideoOrientationLandscapeRight;
    else if ( deviceOrientation == UIDeviceOrientationLandscapeRight )
       result = AVCaptureVideoOrientationLandscapeLeft;
    return result;
}

下面是拍照按钮方法

  1. - (IBAction)takePhotoButtonClick:(UIBarButtonItem *)sender {  
  2.   
  3.     AVCaptureConnection *stillImageConnection = [self.stillImageOutput        connectionWithMediaType:AVMediaTypeVideo];  
  4.     UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];  
  5.     AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation];  
  6.    [stillImageConnection setVideoOrientation:avcaptureOrientation];  
  7.    [stillImageConnection setVideoScaleAndCropFactor:1];  
  8.   
  9.    [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {  
  10.   
  11.         NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];  
  12.   
  13.         //以下是写入手机相册的代码  如果有不写入手机相册的需求  注掉以下代码 就好  
  14.         CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault,  
  15.                                                                 imageDataSampleBuffer,  
  16.                                                                 kCMAttachmentMode_ShouldPropagate);  
  17.   
  18.         ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];  
  19.         if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){  
  20.             //无权限  
  21.             return ;  
  22.         }  
  23.         ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];  
  24.         [library writeImageDataToSavedPhotosAlbum:jpegData metadata:(__bridge id)attachments completionBlock:^(NSURL *assetURL, NSError *error) {  
  25.   
  26.         }];           
  27.         //以上是写入手机相册的代码  如果有不写入手机相册的需求  注掉以下代码 就好  
  28.   
  29.        //这里还有一个问题 就是如果旋转手机后拍照后直接上传到服务器 服务器收到的图片是反的(上下颠倒) 但是如果是在手机相册查看是正常的 所以这里需要做一下处理  
  30.   
  31.        UIImage *img = [UIImage imageWithData:jpegData];  
  32.        if (img.imageOrientation == UIImageOrientationDown) {  
  33.           NSLog(@”照片反了!!!!”);  
  34.           img = [self normalizedImage:img];  
  35.        }  
  36.               
  37.       if (img.imageOrientation == UIImageOrientationUp) {  
  38.           NSLog(@”照片正常!!”);  
  39.        }  
  40.   
  41.      //这里写上传服务器的代码就好了 将img传过去即可  
  42. }];  
  43.   
  44. }  
- (IBAction)takePhotoButtonClick:(UIBarButtonItem *)sender {

    AVCaptureConnection *stillImageConnection = [self.stillImageOutput        connectionWithMediaType:AVMediaTypeVideo];
    UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
    AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation];
   [stillImageConnection setVideoOrientation:avcaptureOrientation];
   [stillImageConnection setVideoScaleAndCropFactor:1];

   [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

        NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

        //以下是写入手机相册的代码  如果有不写入手机相册的需求  注掉以下代码 就好
        CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault,
                                                                imageDataSampleBuffer,
                                                                kCMAttachmentMode_ShouldPropagate);

        ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
        if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){
            //无权限
            return ;
        }
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageDataToSavedPhotosAlbum:jpegData metadata:(__bridge id)attachments completionBlock:^(NSURL *assetURL, NSError *error) {

        }];         
        //以上是写入手机相册的代码  如果有不写入手机相册的需求  注掉以下代码 就好

       //这里还有一个问题 就是如果旋转手机后拍照后直接上传到服务器 服务器收到的图片是反的(上下颠倒) 但是如果是在手机相册查看是正常的 所以这里需要做一下处理

       UIImage *img = [UIImage imageWithData:jpegData];
       if (img.imageOrientation == UIImageOrientationDown) {
          NSLog(@"照片反了!!!!");
          img = [self normalizedImage:img];
       }
            
      if (img.imageOrientation == UIImageOrientationUp) {
          NSLog(@"照片正常!!");
       }

     //这里写上传服务器的代码就好了 将img传过去即可
}];

}


 

  1. - (UIImage *)normalizedImage:(UIImage *)image {  
  2.     if (image.imageOrientation == UIImageOrientationUp){  
  3.         return image;  
  4.     }  
  5.       
  6.     UIGraphicsBeginImageContextWithOptions(image.sizeNO, image.scale);  
  7.     [image drawInRect:(CGRect){00, image.size}];  
  8.     UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();  
  9.     UIGraphicsEndImageContext();  
  10.       
  11.     return normalizedImage;  
  12. }  
- (UIImage *)normalizedImage:(UIImage *)image {
    if (image.imageOrientation == UIImageOrientationUp){
        return image;
    }

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    [image drawInRect:(CGRect){0, 0, image.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return normalizedImage;
}


至此相机的拍照功能已经完成

  • [stillImageConnection setVideoScaleAndCropFactor:1];这个方法是控制焦距用的暂时先固定为1,后边写手势缩放焦距的时候会修改这里
  • 照片写入相册之前需要进行旋转(我在代码里并没有进行旋转)
  • 写入相册之前需要判断用户是否允许了程序访问相册,否则程序会崩溃,包括在开启相机的时候和拍摄按钮点击的时候都需要做安全验证,验证设别是否支持拍照,用户是否允许程序访问相机。

    接下来完成闪光灯

    1. - (IBAction)flashButtonClick:(UIBarButtonItem *)sender {  
    2.   
    3.    NSLog(@”flashButtonClick”);  
    4.   
    5.    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
    6.   
    7.    //修改前必须先锁定  
    8.    [device lockForConfiguration:nil];  
    9.    //必须判定是否有闪光灯,否则如果没有闪光灯会崩溃  
    10.    if ([device hasFlash]) {  
    11.   
    12.       if (device.flashMode == AVCaptureFlashModeOff) {  
    13.         device.flashMode = AVCaptureFlashModeOn;  
    14.   
    15.           [sender setTitle:@”flashOn”];  
    16.       } else if (device.flashMode == AVCaptureFlashModeOn) {  
    17.           device.flashMode = AVCaptureFlashModeAuto;  
    18.           [sender setTitle:@”flashAuto”];  
    19.       } else if (device.flashMode == AVCaptureFlashModeAuto) {  
    20.           device.flashMode = AVCaptureFlashModeOff;  
    21.           [sender setTitle:@”flashOff”];  
    22.       }  
    23.   
    24.    } else {  
    25.   
    26.       NSLog(@”设备不支持闪光灯”);  
    27.    }  
    28.    [device unlockForConfiguration];  
    29. }  
    - (IBAction)flashButtonClick:(UIBarButtonItem *)sender {
    
       NSLog(@"flashButtonClick");
    
       AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
       //修改前必须先锁定
       [device lockForConfiguration:nil];
       //必须判定是否有闪光灯,否则如果没有闪光灯会崩溃
       if ([device hasFlash]) {
    
          if (device.flashMode == AVCaptureFlashModeOff) {
            device.flashMode = AVCaptureFlashModeOn;
    
              [sender setTitle:@"flashOn"];
          } else if (device.flashMode == AVCaptureFlashModeOn) {
              device.flashMode = AVCaptureFlashModeAuto;
              [sender setTitle:@"flashAuto"];
          } else if (device.flashMode == AVCaptureFlashModeAuto) {
              device.flashMode = AVCaptureFlashModeOff;
              [sender setTitle:@"flashOff"];
          }
    
       } else {
    
          NSLog(@"设备不支持闪光灯");
       }
       [device unlockForConfiguration];
    }


    1. </pre><pre>  
    </pre><pre>
    闪光灯的设置非常简单,只需要修改device的flashMode属性即可,这里需要注意的是,修改device时候需要先锁住,修改完成后再解锁,否则会崩溃,设置闪光灯的时候也需要做安全判断,验证设备是否支持闪光灯,有些iOS设备是没有闪光灯的,如果不做判断还是会crash掉 T_T

    剩下一个小功能就是切回镜头了,方法如下

    1. - (IBAction)switchCameraSegmentedControlClick:(UISegmentedControl *)sender {  
    2.   
    3.      NSLog(@”%ld”,(long)sender.selectedSegmentIndex);  
    4.   
    5.     AVCaptureDevicePosition desiredPosition;  
    6.     if (isUsingFrontFacingCamera){  
    7.        desiredPosition = AVCaptureDevicePositionBack;  
    8.     }else{  
    9.        desiredPosition = AVCaptureDevicePositionFront;  
    10.     }  
    11.   
    12.     for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {  
    13.         if ([d position] == desiredPosition) {  
    14.             [self.previewLayer.session beginConfiguration];  
    15.             AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];  
    16.             for (AVCaptureInput *oldInput in self.previewLayer.session.inputs) {  
    17.                [[self.previewLayer session] removeInput:oldInput];  
    18.             }  
    19.             [self.previewLayer.session addInput:input];  
    20.             [self.previewLayer.session commitConfiguration];  
    21.             break;  
    22.         }  
    23.     }  
    24.   
    25.     isUsingFrontFacingCamera = !isUsingFrontFacingCamera;  
    26. }  
    - (IBAction)switchCameraSegmentedControlClick:(UISegmentedControl *)sender {
    
         NSLog(@"%ld",(long)sender.selectedSegmentIndex);
    
        AVCaptureDevicePosition desiredPosition;
        if (isUsingFrontFacingCamera){
           desiredPosition = AVCaptureDevicePositionBack;
        }else{
           desiredPosition = AVCaptureDevicePositionFront;
        }
    
        for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
            if ([d position] == desiredPosition) {
                [self.previewLayer.session beginConfiguration];
                AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
                for (AVCaptureInput *oldInput in self.previewLayer.session.inputs) {
                   [[self.previewLayer session] removeInput:oldInput];
                }
                [self.previewLayer.session addInput:input];
                [self.previewLayer.session commitConfiguration];
                break;
            }
        }
    
        isUsingFrontFacingCamera = !isUsingFrontFacingCamera;
    }

    isUsingFrontFacingCamera这个属性是个BOOL值变量,前面忘记写这个属性了。用于防止重复切换统一摄像头,调用这个点击方法的控件是个segement,文章最后我会附上demo地址。

    最后一步就是加入手势缩放,手动调节相机焦距。
    加入两个属性,并遵守这个协议<UIGestureRecognizerDelegate>

    1. /** 
    2.         *  记录开始的缩放比例 
    3.         */  
    4.       @property(nonatomic,assign)CGFloat beginGestureScale;  
    5.      /** 
    6.       * 最后的缩放比例 
    7.       */  
    8.       @property(nonatomic,assign)CGFloat effectiveScale;  
    /**
            *  记录开始的缩放比例
            */
          @property(nonatomic,assign)CGFloat beginGestureScale;
         /**
          * 最后的缩放比例
          */
          @property(nonatomic,assign)CGFloat effectiveScale;

    这两个属性分别用于记录缩放的比例。相机支持的焦距是1.0~67.5,所以再控制器加载的时候分别给这两个属性附上一个初值 1.0。之后给view添加一个缩放手势,手势调用的方法如下
    1. //缩放手势 用于调整焦距  
    2. - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer{  
    3.   
    4.   BOOL allTouchesAreOnThePreviewLayer = YES;  
    5.   NSUInteger numTouches = [recognizer numberOfTouches], i;  
    6.   for ( i = 0; i < numTouches; ++i ) {  
    7.       CGPoint location = [recognizer locationOfTouch:i inView:self.backView];  
    8.       CGPoint convertedLocation = [self.previewLayer convertPoint:location fromLayer:self.previewLayer.superlayer];  
    9.       if ( ! [self.previewLayer containsPoint:convertedLocation] ) {  
    10.           allTouchesAreOnThePreviewLayer = NO;  
    11.           break;  
    12.        }  
    13. }  
    14.   
    15.    if ( allTouchesAreOnThePreviewLayer ) {  
    16.   
    17.   
    18.        self.effectiveScale = self.beginGestureScale * recognizer.scale;  
    19.        if (self.effectiveScale < 1.0){  
    20.           self.effectiveScale = 1.0;  
    21.        }  
    22.   
    23.        NSLog(@”%f————–>%f————recognizerScale%f”,self.effectiveScale,self.beginGestureScale,recognizer.scale);  
    24.   
    25.        CGFloat maxScaleAndCropFactor = [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor];  
    26.   
    27.        NSLog(@”%f”,maxScaleAndCropFactor);  
    28.        if (self.effectiveScale > maxScaleAndCropFactor)  
    29.         self.effectiveScale = maxScaleAndCropFactor;  
    30.   
    31.        [CATransaction begin];  
    32.        [CATransaction setAnimationDuration:.025];  
    33.        [self.previewLayer setAffineTransform:CGAffineTransformMakeScale(self.effectiveScaleself.effectiveScale)];  
    34.        [CATransaction commit];  
    35.   
    36.     }  
    37.   
    38. }  
    //缩放手势 用于调整焦距
    - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer{
    
      BOOL allTouchesAreOnThePreviewLayer = YES;
      NSUInteger numTouches = [recognizer numberOfTouches], i;
      for ( i = 0; i < numTouches; ++i ) {
          CGPoint location = [recognizer locationOfTouch:i inView:self.backView];
          CGPoint convertedLocation = [self.previewLayer convertPoint:location fromLayer:self.previewLayer.superlayer];
          if ( ! [self.previewLayer containsPoint:convertedLocation] ) {
              allTouchesAreOnThePreviewLayer = NO;
              break;
           }
    }
    
       if ( allTouchesAreOnThePreviewLayer ) {
    
    
           self.effectiveScale = self.beginGestureScale * recognizer.scale;
           if (self.effectiveScale < 1.0){
              self.effectiveScale = 1.0;
           }
    
           NSLog(@"%f-------------->%f------------recognizerScale%f",self.effectiveScale,self.beginGestureScale,recognizer.scale);
    
           CGFloat maxScaleAndCropFactor = [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor];
    
           NSLog(@"%f",maxScaleAndCropFactor);
           if (self.effectiveScale > maxScaleAndCropFactor)
            self.effectiveScale = maxScaleAndCropFactor;
    
           [CATransaction begin];
           [CATransaction setAnimationDuration:.025];
           [self.previewLayer setAffineTransform:CGAffineTransformMakeScale(self.effectiveScale, self.effectiveScale)];
           [CATransaction commit];
    
        }
    
    }

    这样之再实现一个delegate
    1. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer  
    2. {  
    3.    if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] ) {  
    4.        self.beginGestureScale = self.effectiveScale;  
    5.    }  
    6.    return YES;  
    7. }  
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
       if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] ) {
           self.beginGestureScale = self.effectiveScale;
       }
       return YES;
    }

    在每次手势开始的时候把上一次实际缩放值赋给初始缩放值,如果不这么做的话你会发现每次手势开始的时候界面都会跳来跳去的(非常性感)。一个简单功能的相机基本上完成了,最后一步就是之前我们在拍照的方法里写死了一个1.0,我们还需要修改一下它,,否则虽然你看到的界面焦距改变了,但是实际拍出来的照片是没有变化的。找到拍照方法里的
    1. [stillImageConnection setVideoScaleAndCropFactor:1.0];  
    [stillImageConnection setVideoScaleAndCropFactor:1.0];

    修改为
    1. [stillImageConnection setVideoScaleAndCropFactor:self.effectiveScale];  
    [stillImageConnection setVideoScaleAndCropFactor:self.effectiveScale];

  • 如果是横屏开发时候会出现预览层貌似旋转的效果

  • 解决方法:

  • 添加代码

    1. self.previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;  
        self.previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;
    

    注意事项: 如果使用过程中出现 拍摄照片是黑色的(就像是在晚上拍的一样)
  • 是以下代码的使用问题
    1. if (self.session) {  
    2.   
    3.     [self.session startRunning];  
    4.     }  
    if (self.session) {
    
        [self.session startRunning];
        }


  • demo演示下载地址
  • https://github.com/RockyAo/RACustomCamera

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值