2011-06-15 84 views
5

CaptureStillImageAsynchronouslyFromConnection存在一个奇怪的问题。如果在镜像视频时使用jpegStillImageNSDataRepresentation保存图像,则相机胶卷中的图像将顺时针旋转90度。但是,如果它没有镜像,方向就很好。 我会发布代码,任何人都有这个问题/知道修复?AVCaptureMovieFileOutput将照片保存到相机胶卷时导致错误的方向

更新:只是跑了一些测试,高度和宽度(640x480)都很好,反映了设备的方向。当我以纵向拍摄照片时,它会报告UIImageOrientationLeft和镜像时的UIImageOrientationLeftMirrored。

更新2:当我在相机胶卷中查看保存的照片时,图像的预览具有正确的方向,当您在照片之间滑动时的图像也是如此,但是当照片完全加载时,它会旋转90度。这可能是相机胶卷问题吗? (我在4.3.3)

- (void) captureImageAndSaveToCameraRoll 
{ 
    AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]]; 
    if ([stillImageConnection isVideoOrientationSupported]) 
     [stillImageConnection setVideoOrientation:[self orientation]]; 

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection 
                 completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

                  ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) { 
                   if (error) { 
                    if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) { 
                     [[self delegate] captureManager:self didFailWithError:error]; 
                    } 
                   } 
                  }; 

                  if (imageDataSampleBuffer != NULL) { 
                   NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
                   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

                   UIImage *image = [[UIImage alloc] initWithData:imageData]; 
                   [library writeImageToSavedPhotosAlbum:[image CGImage] 
                          orientation:(ALAssetOrientation)[image imageOrientation] 
                         completionBlock:completionBlock]; 
                   [image release]; 

                   [library release]; 
                  } 
                  else 
                   completionBlock(nil, error); 

                  if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) { 
                   [[self delegate] captureManagerStillImageCaptured:self]; 
                  } 
                 }]; 
} 
+0

调试这可能会有所帮助,如果你在这两种模式下输出image.size和imageOrientation,你会得到什么值? – mackross 2011-06-15 01:58:50

+0

只是跑了一些测试,高度和宽度(640x480)都很好,反映了设备的方向。当我以纵向拍摄照片时,它会报告UIImageOrientationLeft和镜像时的UIImageOrientationLeftMirrored。 – 2011-06-15 02:13:21

回答

2

我想知道,如果你新创建的图像确实有它的方向设定为假定你是在这里:

[library writeImageToSavedPhotosAlbum:[image CGImage] orientation: (ALAssetOrientation) [image imageOrientation] completionBlock:completionBlock]; 

[image imageOrientation]特别似乎潜在的问题的根源,特别是需要投.. 。

你说你是什么地方看到的形象定位,是从[image imageOrientation]只是创建后:

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation: imageDataSampleBuffer]; 
UIImage *image = [[UIImage alloc] initWithData:imageData]; 

我想知道你是否假设这个元数据是从AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:返回的imageData,那实际上只包含图像像素数据本身?

+0

请注意,它[特别记录](http://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009722-CH1- SW4)UIImageOrientation可以转换为ALAssetOrientation。 – Anomie 2011-06-25 18:26:07

+0

啊,很好,很棒!它获得了正确的价值吗?正如我上面所说,我想知道元数据是否进入'imageData',然后被解析并添加到'image'中。只是试图追踪数据在哪里开始并在系统中移动,因为它似乎没有得到它需要的地方,如果我理解你的问题。 – Dad 2011-06-26 23:07:03

0

some reports,iPhone的照片应用早在2009年不支持任何在所有镜像取向;他们当然可以部分解决这个问题,但在某些情况下仍然存在缺陷(我知道,例如,在某些情况下,UIImageView不能正确处理contentStretch)。这应该很容易测试:只需将该博客的示例图像加载到相机胶卷中,然后查看会发生什么。

+0

我使用的代码是AVCam 1.0和1.2以及我自己的代码的混合。我想这就是你无法在AVCam 1.2中保存照片的原因?笨。为什么我必须将照片保存为镜像?我想要做的就是保存镜像视频的照片。 – 2011-06-19 20:36:23

+0

在保存到相机胶卷之前,是否有一种简单的方式拍照并垂直翻转? – 2011-06-19 20:37:48

+1

@WDyson:当然,请参阅http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload/5427890#5427890了解上述问题。 – Anomie 2011-06-20 02:15:07

0

创建的UIImage的类别保存到您的图书馆前iOS UIImagePickerController result image orientation after upload

调用图像上这种方法描述。

+0

好奇的事情。我的图像现在全部旋转180度,倒过来。 – 2011-06-20 20:28:17

+0

你将不得不修改这个来做你想做的事。虽然逻辑应该是相似的。 – hundreth 2011-06-20 20:48:18

+1

我冒昧地编辑你的答案,只是链接到我以前对图像旋转的回答,而不是逐字拷贝它。 -1为剽窃,并只是从我发表评论的链接复制你的答案到14小时前的答案。 – Anomie 2011-06-21 18:48:46

相关问题