UIImageView显示动画期间摄像机以错误的方向拍摄的图像

时间:2012-12-12 00:47:45

标签: iphone ios animation camera avfoundation

我正在使用AVFoundation在UIViewController的纵向摄影机视图中使用前置摄像头逐步捕获静止图像。这一切都很好。

我遇到的问题是,当我通过动画显示我在UIImageView内部当前会话中拍摄的图片时,所有图片都显示为横向左侧,我需要将图片显示为拍摄的方向,其中的画像。

我尝试将设备/图像方向强制为肖像画,但没有任何运气。

如果有人能告诉我哪里出错了会很棒。谢谢

我在下面添加了我的代码段。

//拍照并将拍摄的图像存储到可变数组

- (无效)takePicture    {

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];

...

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,然后我在delegate imagePickerController

中更改了我的代码
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    //NSLog(@"%@",info);

    dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
 }  

希望这会对你有所帮助