问题
I'm using AVCapture to create my own camera on iOS. When I click the button to take a photo. I capture the image from the preview layer. Then I assign the captured image to a imageview that I make the size of the screen and place it on top of the preview layer. The problem is the captured image has different proportions to what the ios video camera preview has, so when you click to capture a photo.. You can see the aspect of the photo change. I want the photo to look identical to the preview. I think the way to look at it is that it should look as if the video has paused. So the dimensions of taken image should be identical in proportion to the preview.
Here is me creating the avcapture session:
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];
and then me capturing the image on button click:
- (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];
}
}];
}
if anyone has any ideas that would be greatly appreciated.
来源:https://stackoverflow.com/questions/39350715/avcaptured-image-size-of-screen