拍摄与显示尺寸不同的照片

Capture photo with a different dimension than displayed

我尝试使用 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
)}'

当您准备拍照时,请求 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 返回给您。