我正在尝试根据我的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];
答案 0 :(得分:0)
我相信你需要自己裁剪图像;取景器尺寸的缩小只影响其可见形状,而不影响其处理的数据量。
有关如何裁剪图像的示例,请访问:How to properly crop an image taken on iPhone 4G (with EXIF rotation data)?