Iphone AVFoundation相机尺寸

Iphone AVFoundation相机尺寸,iphone,objective-c,ios6,uiimageview,avfoundation,Iphone,Objective C,Ios6,Uiimageview,Avfoundation,我正在尝试根据UIImageView选择的图像大小设置AVFoundation相机大小,如下所示: self.sess = [AVCaptureSession new]; self.snapper = [AVCaptureStillImageOutput new]; self.snapper.outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG}; self.sess.sessionPreset = AVCapt

我正在尝试根据UIImageView选择的图像大小设置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;但是应用程序中的图像并没有填满屏幕

如何将拍摄图像的大小设置为用户在没有额外尾巴的情况下在相机中看到的大小

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

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

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


有关如何裁剪图像的示例,请访问:

将图像捕获到照片库的代码是什么样子的?@Jacob我在图像下面添加了代码。我正在使用ALAssetLibrary保存它,这样我就可以获取资源URL。这种裁剪不起作用。它创建了一个非常非常高分辨率的图像,这是现有图像的一个小区域,图像看起来是旋转的,尽管我拍摄了它的肖像,有没有一种不裁剪的方法?只需将其设置为用户看到的大小,恐怕您必须自己裁剪。正如我提到的,无法通过AVCaptureStillImageOutput指定尺寸。如果您考虑典型的相机使用,您从相机获得的图像总是全尺寸的,如果您想要裁剪,用户通常自己做。如果您要为他们裁剪,那么您可以在获得图像数据后进行裁剪。上述裁剪方法正确;提供的框架是否正确是不同的;您可能需要四处寻找正确的帧或从何处导出正确的帧。我认为它不会裁剪它,因为图像的比率大于帧大小。因此,我将乘以这些值,然后再次尝试裁剪您的代码,使其无法正常工作1。图像比2。用相机拍摄图像会旋转图像,简单的裁剪不起作用,我必须尝试使用此选项。是否要更新答案?删除裁剪部分,以便我可以接受?