【问题标题】:iOS 10 - Objective-C : How to implement AVCapturePhotoOutput() to capture image and videos?iOS 10 - Objective-C:如何实现 AVCapturePhotoOutput() 来捕获图像和视频?
【发布时间】:2017-02-13 19:31:41
【问题描述】:

我正在尝试从我的应用中捕获图像和视频,现在从 iOS 10 开始,"AVCaptureStillImageOutput" 已被弃用。

请帮助我在 Objective-C 中实现 AVCapturePhotoOutput

这是我的示例代码:

_avCaptureOutput = [[AVCapturePhotoOutput alloc]init];
_avSettings = [AVCapturePhotoSettings photoSettings];


AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
[captureSession startRunning];



AVCaptureConnection *connection = [self.movieOutput connectionWithMediaType:AVMediaTypeVideo];

if (connection.active)
{
    //connection is active
    NSLog(@"Connection is active");

    id previewPixelType = _avSettings.availablePreviewPhotoPixelFormatTypes.firstObject;
    NSDictionary *format = @{(NSString*)kCVPixelBufferPixelFormatTypeKey:previewPixelType,(NSString*)kCVPixelBufferWidthKey:@160,(NSString*)kCVPixelBufferHeightKey:@160};

    _avSettings.previewPhotoFormat = format;

    [_avCaptureOutput capturePhotoWithSettings:_avSettings delegate:self];


}
else
{
    NSLog(@"Connection is not active");
    //connection is not active
    //try to change self.captureSession.sessionPreset,
    //or change videoDevice.activeFormat
}

【问题讨论】:

  • AVCapturePhotoOutput 的用途是什么?
  • 你实现AVCapturePhotoCaptureDelegate了吗?
  • self.movieOutput是什么类型?
  • self.movi​​eOutput 是一种 AVCaptureMovieFileOutput 类。是的,我确实实现了 AVCapturePhotoCaptureDelegate 方法。

标签: ios objective-c iphone ios10 avcapturesession


【解决方案1】:
_avCaptureOutput = [[AVCapturePhotoOutput alloc]init];
_avSettings = [AVCapturePhotoSettings photoSettings];

AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
[captureSession startRunning];

[self.avCaptureOutput capturePhotoWithSettings:self.avSettings delegate:self];

self 必须实现 AVCapturePhotoCaptureDelegate

#pragma mark - AVCapturePhotoCaptureDelegate
-(void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(AVCaptureBracketedStillImageSettings *)bracketSettings error:(NSError *)error
{
    if (error) {
        NSLog(@"error : %@", error.localizedDescription);
    }

    if (photoSampleBuffer) {
        NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
        UIImage *image = [UIImage imageWithData:data];
    }
}

现在,你得到图像,然后做任何你想做的事情。


注意:由于 iOS 11,-captureOutput:didFinishProcessingPhotoSampleBuffer:... 已弃用,需要改用-captureOutput:didFinishProcessingPhoto:error:

- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(nullable NSError *)error
{  
  NSData *imageData = [photo fileDataRepresentation];
  UIImage *image = [UIImage imageWithData:data];
  ...
}

【讨论】:

  • 真的很容易实现
  • 如何从中获取预览照片?
猜你喜欢
  • 2017-02-12
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
相关资源
最近更新 更多