如何使用avFoundation在iPhone中拍摄方形照片,就像在Instagram应用程序中一样



我正在像这里一样用AVFoundation拍照:http://red-glasses.com/index.php/tutorials/ios4-take-photos-with-live-video-preview-using-avfoundation/

一个片段:

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
     if (exifAttachments)
     {
         // Do something with the attachments.
         NSLog(@"attachements: %@", exifAttachments);
     }
     else
         NSLog(@"no attachments");
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];

据我所知,图片尺寸设置在这里:

   AVCaptureSession *session = [[AVCaptureSession alloc] init];  
   session.sessionPreset =AVCaptureSessionPresetMedium; //medium on my iphone4 is 640*480

但这里只有少数可能的设置,不能设置为自定义大小。

如果可能的话,我希望拍摄的照片具有 1:1 的纵横比,例如 400x400 像素(方形图片)。似乎Instagram正在这样做(或者他们正在裁剪图像?

如何将拍摄的照片的大小指定为正方形?我知道如何在手机上更改大小,但是如果拍摄的照片不是方形的,则结果不好。

知道吗?

您可能应该通过创建新上下文来调整 UIImage 的大小。以下示例

.....
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImage *tempImage = nil;
CGSize targetSize = CGSizeMake(400,400);
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
thumbnailRect.origin = CGPointMake(0.0,0.0);
thumbnailRect.size.width  = targetSize.width;
thumbnailRect.size.height = targetSize.height;
[image drawInRect:thumbnailRect];
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// tmpImage is resized to 400x400

希望这有帮助!

我找到了一个使用AVCaptureSession拍照的代码,这是"JJob"的一个很棒的教程。这是关于AVCaptureSession的最佳教程;请阅读本网站的所有评论和以前的教程,这将对您有很大帮助。您可以从此处下载工作示例代码。

相关内容

最新更新