将照片保存到相机胶卷时,AVCaptureMovieFileOutput 导致方向错误

发布于 2024-11-15 08:37:09 字数 3419 浏览 6 评论 0原文

我对 captureStillImageAsynchronouslyFromConnection 有一个奇怪的问题。如果我在视频镜像时使用 jpegStillImageNSDataRepresentation 保存图像,则相机胶卷中的图像将顺时针旋转 90 度。但是,如果没有镜像,方向就很好。 我将发布代码,其他人有这个问题/知道修复吗?

更新:刚刚运行了一些测试,高度和宽度(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];
                                                             }
                                                         }];
}

I have an odd problem with captureStillImageAsynchronouslyFromConnection. If I save the image using jpegStillImageNSDataRepresentation while the video is mirrored, the image in the camera roll is rotated 90 degrees clockwise. However, if it's not mirrored, the orientation is fine.
I'll post the code, anyone else have this problem/know of a fix?

Update: Just ran some tests, the heights and widths (640x480) are fine and reflect the device's orientation. When I Take a picture in portrait, it reports UIImageOrientationLeft and when mirrored, UIImageOrientationLeftMirrored.

Update 2: When I view the saved photo in the camera roll, the preview of the image has the right orientation, as does the image when you swipe between photos, but when the photo is fully loaded, it rotates 90 degrees. Could this be a camera roll problem? (I'm on 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];
                                                             }
                                                         }];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山川志 2024-11-22 08:37:09

我想知道您新创建的图像是否确实具有您在此处假设的方向设置:

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

我想知道您是否假设此元数据位于 imageData 返回自AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:实际上何时仅包含图像像素数据本身?

I'm wondering if you the newly created image really has it's orientation set as you are assuming here:

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

[image imageOrientation] in particular seems potentially the source of the problem, especially with the cast required...

you say you are seeing the image orientation somewhere, is it from [image imageOrientation] just after creating it with:

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

I'm wondering if you are assuming that this metadata is in the imageData returned from AVCaptureStillImageOutput jpegStillImageNSDataRepresentation: when that in fact only contains the image pixel data itself?

饮惑 2024-11-22 08:37:09

根据一些报告,早在 2009 年 iPhone 的照片应用程序就不支持任何镜像方向;他们当然有可能部分修复了这个问题,但在某些情况下仍然存在错误(例如,我知道 UIImageView 在某些情况下无法正确处理 contentStretch )。这应该很容易测试:只需将该博客中的示例图像加载到相机胶卷中,然后看看会发生什么。

According to some reports, the iPhone's photos application back in 2009 didn't support any of the mirrored orientations at all; it's certainly possible that they partially fixed this but still have bugs in some cases (I know, for example, that UIImageView doesn't handle contentStretch correctly in some cases). This should be easy enough to test: just load the sample images from that blog into the camera roll and see what happens.

冷血 2024-11-22 08:37:09

创建 iOS UIImagePickerController 结果上传后图像方向中描述的 UIImage 类别

在将图像保存到库之前调用此方法。

Create the category for UIImage described in iOS UIImagePickerController result image orientation after upload

Call this method on the image before saving it to your library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文