9

私はここのようにAVFoundationで写真を撮っています:http: //red-glasses.com/index.php/tutorials/ios4-take-photos-with-live-video-preview-using-avfoundation/

1つのスニペット:

[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

ただし、可能な設定はごくわずかであり、カスタムサイズに設定することはできません。

可能であれば、400x400ピクセル(正方形の写真)のように、アスペクト比を1:1にして写真を撮りたいと思います。Instagramがそれをやっているようです(または彼らは画像をトリミングしていますか?)。

撮影した写真のサイズを正方形に指定するにはどうすればよいですか?携帯電話でサイズを変更する方法は知っていますが、撮影した写真が正方形でない場合、結果は良くありません。

何か案が?

4

2 に答える 2

12

おそらく、新しいコンテキストを作成して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

お役に立てれば !

于 2012-06-07T10:24:15.723 に答える
0

AVCaptureSessionを使用して写真を撮るためのコードを1つ見つけました。これは、「JJob」によるすばらしいチュートリアルです。これは、AVCaptureSessionに関する最高のチュートリアルです。このサイトのすべてのコメントと以前のチュートリアルを読んでください。これは大いに役立ちます。ここから実用的なサンプルコードをダウンロードできます。

于 2012-06-07T10:34:56.053 に答える