3

I'm capturing an image using AVFoundation. I'm using AVCaptureVideoPreviewLayer to display the camera feed on screen. This preview layer's frame gets the bounds of a UIView with dynamic dimensions:

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [self.cameraFeedView layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.cameraFeedView.frame;
[previewLayer setFrame:frame];
previewLayer.frame = rootLayer.bounds;
[rootLayer insertSublayer:previewLayer atIndex:0];

And I'm using AVCaptureStillImageOutput to capture an image:

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                              completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                  if (imageDataSampleBuffer != NULL) {
                                                      NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                      UIImage *capturedImage = [UIImage imageWithData:imageData];
                                                  }
                                              }];

My problem is that the captured image is at the size of the iPhone camera (1280x960 - front camera), but I need it to be the same aspect ratio as the preview layer. For example, if the size of the preview layer is 150x100, I need the captured image to be 960x640. Is there any solution for this?

2shy
  • 318
  • 3
  • 15
  • Try to resize the image – srinivas n Jul 29 '15 at 12:59
  • http://stackoverflow.com/questions/612131/whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk – srinivas n Jul 29 '15 at 12:59
  • http://stackoverflow.com/questions/17018617/how-to-resize-an-image-in-ios – srinivas n Jul 29 '15 at 13:00
  • I tried resizing already, but because the aspect ratio is different, the resized image looks squished. I can crop the image, but isn't there a way to just get the original frame from the preview layer? – 2shy Jul 29 '15 at 13:13

1 Answers1

1

I also enter counter the same problem. You have to crop or resize output still image. But you should notice that output still`s scale and image orientation.

preview layer square frame

CGFloat width = CGRectGetWidth(self.view.bounds);
[self.captureVideoPreviewLayer setFrame:CGRectMake(0, 0, width, width)];
[self.cameraView.layer addSublayer:self.captureVideoPreviewLayer];

calculate cropped image`s frame

[self.captureStillImageOutput captureStillImageAsynchronouslyFromConnection:captureConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
    NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
    UIImage *image = [[UIImage alloc] initWithData:data];

    CGRect cropRect = CGRectMake((image.size.height - image.size.width) / 2, 0, image.size.width, image.size.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation]; // always UIImageOrientationRight
    CGImageRelease(imageRef);
}];
Dan Jiang
  • 89
  • 7