AVFoundation相机尺寸

时间:2013-01-28 18:02:46

标签: iphone objective-c ios6 uiimageview avfoundation

我正在尝试根据我的UIImageView(selectedImage)大小设置AVFoundation相机大小,如下所示:

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

在应用程序中,它显示它很好 - 尺寸为320X320,但当我查看照片库时,保存的图像比一个正方形长。

我还尝试删除lay.videoGravity = AVLayerVideoGravityResizeAspectFill;但是应用中的图片没有填满屏幕。

如何将拍摄的图像尺寸设置为用户在相机中看到的没有额外尾部的尺寸?

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

这是将图片保存到图库的代码:

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

1 个答案:

答案 0 :(得分:0)

我相信你需要自己裁剪图像;取景器尺寸的缩小只影响其可见形状,而不影响其处理的数据量。

有关如何裁剪图像的示例,请访问:How to properly crop an image taken on iPhone 4G (with EXIF rotation data)?