0

There seems to be a number of questions about this issue here. Since most of them don’t provide code snippets, I am electing to show all my code. The problem: the picture taken is taller than the preview. My preview is w=288 by h=288; but the picture that is returned is w=2448 by h=3264. Why is the returned image not a square when my preview is a square?

AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    session=[[AVCaptureSession alloc]init];
    [session setSessionPreset:AVCaptureSessionPresetPhoto];
    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error;
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

    if ([session canAddInput:deviceInput]) {
        [session addInput:deviceInput];
    }

    AVCaptureVideoPreviewLayer * previewLayout = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
    [previewLayout setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayout =[[self view]layer];
    [rootLayout setMasksToBounds:YES];
    CGRect frame = self.frameForCapture.frame;
    [previewLayout setFrame:frame];
    [rootLayout insertSublayer:previewLayout atIndex:0];

    stillImageOutput=[[AVCaptureStillImageOutput alloc]init];

    NSDictionary *outputSettings =[[NSDictionary alloc]initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];
    [session addOutput:stillImageOutput];

    [session startRunning];
}

- (IBAction)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 != NULL) {//I.E. it does exist
                                                          NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                          UIImage *originalImage =[UIImage imageWithData:imageData ];
                                                          NSLog(@"Image w = %f; h = %f",originalImage.size.width,originalImage.size.height);
                                                          CGSize size = self.frameForCapture.frame.size;
                                                          UIGraphicsBeginImageContext(size);
                                                          [originalImage drawInRect:CGRectMake(0,0,size.width,size.width)];
                                                          UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
                                                          UIGraphicsEndImageContext();
                                                          self.imageView.image=image;
                                                          [self.imageView setHidden:NO];
                                                          [self.frameForCapture setHidden:YES];
                                                      }
                                                  }];
}

I have been playing around with CGImageCreateWithImageInRect to get the preview to be what I finally save and use in my ImageView. But so far no luck. One of the places I have been looking is Cropping an UIImage. Any ideas how I might fix this issue?

Community
  • 1
  • 1
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • What values does this line print: `NSLog(@"Image w = %f; h = %f",originalImage.size.width,originalImage.size.height);`? w=288 by h=288 or w=2448 by h=3264 – Lyndsey Scott Jan 10 '15 at 18:40
  • It prints w=2448 by h=3264; which is a rectangle. In any case, when I try to crop into a square, I get into all sorts of problems. The image comes out pushed down,etc. I try using http://stackoverflow.com/questions/158914/cropping-a-uiimage but not much yet. – Katedral Pillon Jan 10 '15 at 18:50
  • 1
    This appears to be relevant to your question. http://stackoverflow.com/questions/15951746/how-to-crop-an-image-from-avcapture-to-a-rect-seen-on-the-display – Brad Brighton Jan 10 '15 at 19:17
  • 1
    The original answer isn't my code so I'm not 100% certain, but seems to be `previewLayout` in your code (which is of type AVCaptureVideoPreviewLayer, and appears to be where you define the result that you ultimately want to achieve). – Brad Brighton Jan 10 '15 at 19:29

0 Answers0