我正在尝试从 iPad 上的视频中截取图片。我使用 Apple 的 AVCam 示例作为起点。
我能够在我的应用程序中看到视频并从中拍照。我的问题是结果图像的像素大小错误。我想要一张全屏图片 (1024x768),但我得到一张较小的图片 (1024x720)。
这些是我的实例变量:
@property (retain) AVCaptureStillImageOutput *stillImageOutput;
@property (retain) AVCaptureVideoPreviewLayer *previewLayer;
@property (retain) AVCaptureSession *captureSession;
@property (retain) AVCaptureConnection *captureConnection;
@property (retain) UIImage *stillImage;
这里是拍照代码:
- (void)takePicture
{
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments) {
NSLog(@"attachements: %@", exifAttachments);
} else {
NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self setStillImage:image];
[image release];
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
}];
}
我想调整最终图片的大小,但此解决方案会降低图片质量。
我还意识到 CFDictionaryRef exifAttachments
字典包含一个值 PixelYDimension = 720;
但我似乎找不到与之交互的方法。
如有任何帮助,我们将不胜感激。 提前谢谢你,祝你有美好的一天,
亚历克斯。
编辑:我想指出的是,当我说“从视频中拍摄照片”时,我的意思是视频是从 iPad 的摄像头实时拍摄的,它不是 一段录音。
最佳答案
我找到了解决问题的方法。保存在这里,以防将来有人查找此内容。
要使用 AVFoundation 与相机交互,我们需要启动一个 AVCaptureSession
变量。
这样做之后我们可以修改 sessionPreset指示输出的质量级别或比特率。
有一个set of different constants .为了拍摄 1024x768 的照片,我使用了 AVCaptureSessionPresetPhoto
关于ios - 使用 AVFoundation 从视频中捕获图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683916/