相机捕捉比预览更高的图像

时间:2015-01-10 18:32:23

标签: ios iphone camera uiimage

这里似乎有很多关于这个问题的问题。由于他们中的大多数都不提供代码片段,我选择显示我的所有代码。问题:拍摄的照片比预览的要高。我的预览是w = 288,h = 288;但是返回的图片是w = 2448,h = 3264。当我的预览是正方形时,为什么返回的图像不是正方形?

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];
                                                      }
                                                  }];
}

我一直在玩CGImageCreateWithImageInRect以使预览成为我最终在我的ImageView中保存和使用的内容。但到目前为止还没有运气。我一直在寻找的地方之一是Cropping an UIImage。我有什么想法可以解决这个问题吗?

0 个答案:

没有答案