我正在使用AVCapture在iOS上创建自己的相机。当我点击按钮拍照时。我从预览图层捕获图像。然后我将捕获的图像分配给我制作屏幕大小的图像视图,并将其放在预览图层的顶部。问题是捕获的图像与ios摄像机预览具有不同的比例,因此当您单击以捕获照片时......您可以看到照片的更改方面。我希望照片看起来与预览相同。我认为看待它的方式是它应该看起来好像视频暂停了。因此,拍摄图像的尺寸应与预览成比例。
这是我创建avcapture会话:
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *photoTaken = [[UIImageView alloc] initWithFrame:self.view.frame];
photoTaken.image = image;
photoTaken.contentMode = UIViewContentModeScaleAspectFit;
self.takenPhoto = photoTaken;
[self.view.layer insertSublayer:self.takenPhoto.layer atIndex:1];
self.takenPhoto.layer.name = @"imageLayer";
[self imageTaken];
然后我点击按钮点击图像:
- (void)takePhoto:(id)sender {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *photoTaken = [[UIImageView alloc] initWithFrame:self.view.frame];
photoTaken.image = image;
photoTaken.contentMode = UIViewContentModeScaleAspectFit;
self.takenPhoto = photoTaken;
[self.view.layer insertSublayer:self.takenPhoto.layer atIndex:1];
self.takenPhoto.layer.name = @"imageLayer";
[self imageTaken];
}
}];
}
如果有人有任何想法会非常感激。