我正在尝试根据我的 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];