7

重要提示:如果我使用: session.sessionPreset = AVCaptureSessionPresetHigh;我的预览图像没有被拉伸!如果我将照片保存到设备UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);上,图像是正常的,只有在预览中才会被拉伸。

我正在使用 AVFoundation 来拍摄照片。

session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;

CALayer *viewLayer = vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = vImagePreview.bounds;
[vImagePreview.layer addSublayer:captureVideoPreviewLayer];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

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

我将 sessionPreset 设置为AVCaptureSessionPresetPhoto

session.sessionPreset = AVCaptureSessionPresetPhoto;

我的捕获方法:

-(void)captureNow {
    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;
        }
    }

    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
         if (exifAttachments)
         {
             // Do something with the attachments.
             NSLog(@"attachements: %@", exifAttachments);
         } else {
             NSLog(@"no attachments");
         }

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         UIImage *image = [[UIImage alloc] initWithData:imageData];
         NSLog(@"%@",NSStringFromCGSize(image.size));
         [self animateUpTheImageWithImage:image];


     }];

}

我在哪里添加捕获的照片的预览:

- (void) animateUpTheImageWithImage:(UIImage*)theImage{

    UIView* preview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/*426*/)];
    CALayer *previewLayer = preview.layer;
    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    captureVideoPreviewLayer.frame = previewLayer.frame;
    [previewLayer addSublayer:captureVideoPreviewLayer];
    captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;

    [self addSubview:preview];

}

结果是我捕获的图像被拉伸了!

捕捉相机预览

捕获的图像预览

4

3 回答 3

14

所以我解决了我的问题。这是我现在正在使用的代码,它工作正常:

    session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetHigh;

    captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    captureVideoPreviewLayer.frame = vImagePreview.bounds;
    [vImagePreview.layer addSublayer:captureVideoPreviewLayer];

    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

...

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

capturedImageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.screenWidth,self.screenHeight)];
[self addSubview:capturedImageView];

和重要的输出imagaView:

vImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.screenWidth,self.screenHeight)];
    [capturedImageView addSubview:vImage];
    vImage.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth);
    vImage.contentMode = UIViewContentModeScaleAspectFill;
    vImage.image = theImage;

一些附加信息:

相机层必须是全屏的(你可以修改它的 y 坐标,但宽度和高度必须是全尺寸)并且 outputImageView 也必须是。

我希望这些对某些人来说也是有用的信息。

于 2014-02-21T08:34:55.743 回答
0

尝试使用[captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

于 2013-10-04T10:57:22.597 回答
0

将捕获 sessionPreset 更改为 AVCaptureSessionPresetiFrame960x540;它对我很有效。

于 2015-06-18T09:25:47.363 回答