关于ios:iOS拍照定制之AVCapturePhotoOutput

40次阅读

共计 1855 个字符,预计需要花费 5 分钟才能阅读完成。

  • 问题

    领导安顿工作,写个拍照性能,界面跟零碎拍照有点出入

    拍完照片,底部显示已拍照片,有个拍照下限 [在此不管]

    点击已拍照片,能够预览、放大放大查看

  • 思路

    零碎拍照必定不行了,只能定制,没提是否拍照禁止声音的需要 [非偷拍,正经人]

    原则上能简略就不简单,拍砖 AVCapturePhotoOutput

  • 捋捋

    首先,你须要一块临时以后场景的 layer,这里不提,参考上篇 [iOS 写在定制相机之前]

    其次,定义 session + photoOutput

    再次,定义拍照事件

    最初,代理中取得照片

  • 上菜

    • session 定义 input 设置

      self.session = [[AVCaptureSession alloc] init];
      self.session.sessionPreset = AVCaptureSessionPresetPhoto;
      AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
      self.dInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
      if ([self.session canAddInput:self.dInput]) {[self.session addInput:self.dInput];
      }
    • 拍照场景 layer

      self.preView = [[AVPreView alloc] init];
      self.preView.backgroundColor = [UIColor colorWithHexs:0x3f3f3f];
      self.preView.session = self.session;
      [self.preView setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
      [self.view addSubview:self.preView];
      [self.preView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.view);
          make.top.equalTo(self.topView.mas_bottom);
          make.bottom.equalTo(self.collect.mas_top);
          make.right.equalTo(rightView.mas_left);
      }];
    • 设置 photoOutput

      self.photoOutput = [[AVCapturePhotoOutput alloc] init];
      if ([self.session canAddOutput:self.photoOutput]) {[self.session addOutput:self.photoOutput];
      }
      [self.photoOutput.connections.lastObject setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    • session start

      [self.session startRunning];
    • 拍照点击

      AVCapturePhotoSettings *set = [AVCapturePhotoSettings photoSettings];
      [self.photoOutput capturePhotoWithSettings:set delegate:self];
    • AVCapturePhotoCaptureDelegate

      #pragma mark - AVCapturePhotoCaptureDelegate
      -(void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error  API_AVAILABLE(ios(11.0)) {if (!error) {
              // 应用该形式获取图片,可能图片会存在旋转问题,在应用的时候调整图片即可
              NSData *data = [photo fileDataRepresentation];
              UIImage *image = [UIImage imageWithData:data];
              
              // 对,就是下面的 image
          }
      }
      
  • Game Over.

    See me Tomorrow, haha

正文完
 0