私はAVFoundationを使用して、UIViewControllerの縦向きカメラビューでフロントカメラでプログラム的に静止画像をキャプチャしています。これはすべて良いです。
私が抱えている問題は、現在のセッションで UIImageView 内で撮影した写真をアニメーションで表示するときに、すべての写真が横向きのまま表示されることです。縦向きで撮影された向きで写真を表示する必要があります。
デバイス/画像の向きを強制的に縦向きにしようとしましたが、うまくいきませんでした。
誰かが私が間違っている場所を教えてくれれば素晴らしいでしょう。ありがとうございました
以下に私のコードスニペットを含めました。
//写真を撮り、撮った画像をミュータブル配列に保存する
-(ボイド)テイクピクチャー{
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
if ([videoConnection isVideoOrientationSupported])
[videoConnection setVideoOrientation:[UIDevice currentDevice].orientation];
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];
UIImage *orientedImage= [[UIImage alloc] initWithCGImage: image.CGImage scale:1.0 orientation:UIImageOrientationUp];
NSLog(@"the image oreintation is %i", orientedImage.imageOrientation);
[photosTakenArray addObject:orientedImage];
...
これは、UIImageView startAnimating 関数を使用して、別の UIViewController 内でカメラが撮影した画像を表示する方法です。これは、カメラで撮影した写真がすべて横向きのまま表示されるという問題です。この場合、photoViewer は私の UIImageView であり、photosToShow は上記の photosTakenArray への NSArray 参照です。
if ([photosToShow count]>0) {
NSLog(@"we have %i photos to display", [photosToShow count]);
photoViewer.animationImages = photosToShow;
// all frames will execute in 1.75 seconds
photoViewer.animationDuration = 1.75;
// repeat the annimation forever
photoViewer.animationRepeatCount = 0;
// start animating
[photoViewer startAnimating];
...