1

I am trying to set the AVFoundation camera size based on my UIImageView (selectedImage) size like this:

    self.sess = [AVCaptureSession new];
    self.snapper = [AVCaptureStillImageOutput new];
    self.snapper.outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG};
    self.sess.sessionPreset = AVCaptureSessionPresetPhoto;
    [self.sess addOutput:self.snapper];
    AVCaptureDevice* cam = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:cam error:nil];
    [self.sess addInput:input];
    AVCaptureVideoPreviewLayer* lay = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.sess];
    lay.videoGravity = AVLayerVideoGravityResizeAspectFill;
    lay.frame = selectedImage.frame;
   [self.view.layer addSublayer:lay];

In the app, it shows it just fine - 320X320 size, but when I look in the photo gallery, the saved image is longer than a square.

I also tried to remove lay.videoGravity = AVLayerVideoGravityResizeAspectFill; but then the image in the app is not filling the screen.

How can I set also the shot image size to be what the user sees in the camera with no extra tails?

the image as it looks after capturing in the app the image how it is saved in the photo album camera image no aspect fill

This is the code that saves the image to gallery:

AVCaptureConnection *vc = [self.snapper connectionWithMediaType:AVMediaTypeVideo];
// deal with image when it arrives
typedef void(^MyBufBlock)(CMSampleBufferRef, NSError*);
MyBufBlock h = ^(CMSampleBufferRef buf, NSError *err) {
    NSData* data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf];
    UIImage* im = [UIImage imageWithData:data];
    dispatch_async(dispatch_get_main_queue(), ^{
        selectedImage.hidden = NO;
        selectedImage.contentMode = UIViewContentModeScaleAspectFill;
        selectedImage.clipsToBounds = YES;
        selectedImage.image = im;
        [self.previewLayer removeFromSuperlayer];
        self.previewLayer = nil;
        [self.sess stopRunning];
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:[selectedImage.image CGImage] orientation:(ALAssetOrientation)[selectedImage.image imageOrientation] completionBlock:nil];
Dejell
  • 13,947
  • 40
  • 146
  • 229

1 Answers1

0

I believe you'll need to crop the image yourself; the reduced size of your viewfinder only affected its visible shape, not the amount of data it would be processing.

An example on how to crop an image can be found at: How to properly crop an image taken on iPhone 4G (with EXIF rotation data)?

Community
  • 1
  • 1
WDUK
  • 18,870
  • 3
  • 64
  • 72
  • This cropping doesn't work. It creates a very very high resolution image, which is a small area of the exiting one and the image looks rotated although I took it in Portrait – Dejell Jan 29 '13 at 06:16
  • And is there a way to do it without cropping? just to set it to the size that the user sees – Dejell Jan 29 '13 at 06:25
  • I'm afraid you'll have to crop it yourself. As I mentioned, it's not possible to specify your dimensions via AVCaptureStillImageOutput. If you think about typical camera use, the image you get from the camera is always full size, and if you want it cropped, the user usually does it themselves. If you're cropping it for them, then you can do that after you have the image data. The method for cropping above is correct; whether the frame provided (`lay.frame`) is correct is different; you might need to play around to find the correct frame (or where to derive the correct frame). – WDUK Jan 29 '13 at 09:52
  • I think that it doesn't crop it as the ratio of the image is bigger than the frame size. so I will multiply the values and try again – Dejell Jan 29 '13 at 09:56
  • Your code to crop doesn't work well 1. Image ratio 2. Taking an image with the camera rotates it and simple cropping wouldn't work I will have to try to use this http://stackoverflow.com/questions/7203847/how-to-properly-crop-an-image-taken-on-iphone-4g-with-exif-rotation-data Would you like to update your answer (remove the cropping part) so I could accept it? – Dejell Feb 03 '13 at 10:56
  • @Odelya Done, apologies about that – WDUK Feb 03 '13 at 17:39