1

我正在尝试根据我的 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;,但应用程序中的图像没有填满屏幕。

如何将拍摄的图像大小设置为用户在相机中看到的没有多余的尾巴?

在应用程序中捕获后的图像 图像如何保存在相册中 相机图像无纵横比填充

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

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

1 回答 1

0

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

有关如何裁剪图像的示例,请参见:如何正确裁剪在 iPhone 4G 上拍摄的图像(带有 EXIF 旋转数据)?

于 2013-01-28T20:51:51.513 回答