问题描述
我尝试使用AVFoundation从iPhone捕获图片,但是在寻找所需内容时遇到了一些麻烦.我创建了一个AVCaptureSession并将预设添加到AVCaptureSessionPresetHigh.self.session.sessionPreset = AVCaptureSessionPresetHigh;
I try to capture picture from a iPhone with AVFoundation but I have some troubles to find what I need.I created a AVCaptureSession and add preset to AVCaptureSessionPresetHigh.self.session.sessionPreset = AVCaptureSessionPresetHigh;
我在视图中显示videoPreviewLayer,并且可以使用以下代码捕获图片:
I display the videoPreviewLayer in my view and I can capture the picture with the code:
AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettingsWithFormat: @{AVVideoCodecKey:AVVideoCodecJPEG}];
[self.stillImageOutput capturePhotoWithSettings:settings delegate:self]
我得到了FHD图片(1920x1080),所以效果很好.
And I get a FHD picture (1920x1080), so this works well.
但是现在我要保持良好的显示图片,因此请保持较高的预设值,但要以640x480的分辨率捕获图片.
But now I want to keep a good displayed picture, so keep the preset high but capture picture with the resolution 640x480.
有可能吗?有什么想法吗?
Is it possible?Any ideas?
我尝试过:
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的辅助输出,如下所示:
When you are preparing to take the photo, request a 640x480 secondary output, like this:
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
返回给您.
The secondary output will come back to you as the photo’s previewCGImageRepresentation
in the delegate method.
这篇关于捕获尺寸与显示尺寸不同的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!