捕获与显示尺寸不同的照片

时间:2019-03-13 20:42:05

标签: objective-c avfoundation

我尝试使用AVFoundation从iPhone捕获图片,但是在寻找所需内容时遇到了一些麻烦。 我创建了一个AVCaptureSession并将预设添加到AVCaptureSessionPresetHigh。 self.session.sessionPreset = AVCaptureSessionPresetHigh;

我在视图中显示videoPreviewLayer,并且可以使用以下代码捕获图片:

AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettingsWithFormat: @{AVVideoCodecKey:AVVideoCodecJPEG}];
[self.stillImageOutput capturePhotoWithSettings:settings delegate:self]

我得到了FHD图片(1920x1080),所以效果很好。

但是现在我要保持良好的显示图片,因此请保持较高的预设值,但要以640x480的分辨率捕获图片。

有可能吗? 有什么想法吗?


我尝试过:

NSDictionary *outputSetting = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithDouble:1280], (id)kCVPixelBufferHeightKey,
                               [NSNumber numberWithDouble:960], (id)kCVPixelBufferWidthKey,
                               [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,
                               nil];

AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettingsWithFormat: outputSetting];

[self.stillImageOutput capturePhotoWithSettings:settings delegate:(id)self];

但是我得到这个错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[AVCapturePhotoSettings photoSettingsWithFormat:] Unsupported keys specified: {(
    Height,
    Width
)}. Supported keys are {(
    PixelFormatType
)}'

1 个答案:

答案 0 :(得分:0)

准备拍照时,请请求640x480的辅助输出,如下所示:

let settings = AVCapturePhotoSettings()
let pbpf = settings.availablePreviewPhotoPixelFormatTypes[0]
settings.previewPhotoFormat = [
    kCVPixelBufferPixelFormatTypeKey as String : pbpf,
    kCVPixelBufferWidthKey as String : 640,
    kCVPixelBufferHeightKey as String : 480

拍照时,像这样传递settings

output.capturePhoto(with: settings, delegate: self)

辅助输出将作为委托方法中照片的previewCGImageRepresentation返回给您。