ALAsset 中的照片方向行为不正确

发布于 2024-12-26 05:47:40 字数 531 浏览 1 评论 0原文

我当前有一个使用 ALAsssetsLibrary 来获取照片的应用程序。我已将照片放置到图像视图中,并且可以上传到服务器。拍了几张照片后在真机上测试时,发现原本应该拍人像的照片变成了风景。

因此,我调用了不同的函数来获取 CGImage,如下所示:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0 orientation:(UIImageOrientation)[representation orientation]];

第一次尝试,我使用了这个:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]]

我认为具有比例和方向的函数可以为我提供拍摄照片的正确方向。但它没有给我正确的解决方案。

我是否错过了生成正确照片方向所需的任何内容?

I current have an app that uses ALAsssetsLibrary to fetch the photos. I have placed the photo to an image view and I am able to upload to the server. When I tested on the real device after taking some photos, I found out the photos that supposed to be taken in Portrait become a landscape.

Therefore, I called different function to get the CGImage like this:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0 orientation:(UIImageOrientation)[representation orientation]];

The first tried out, I used this :

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]]

I thought the one with scale and orientation could give me the right orientation that the photo was taken. But it didn't give me the right solution.

Do I miss anything that is necessary to generate a correct orientation of photo?

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

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

发布评论

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

评论(4

ㄟ。诗瑗 2025-01-02 05:47:40

正确的方向处理取决于您使用的 iOS 版本。
在 iOS4 和 iOS 5 上,缩略图已经正确旋转,因此您可以在不指定任何旋转参数的情况下初始化 UIImage。
但是,对于 fullScreenImage,每个 iOS 版本的行为都不同。在 iOS 5 上,图像已经旋转,但在 iOS 4 上则不然。

因此,在 iOS4 上,您应该使用:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
                                                     scale:[defaultRep scale] orientation:(UIImageOrientation)[defaultRep orientation]];

在 iOS5 上,以下代码应该可以正常工作:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];

干杯,

Hendrik

The correct orientation handling depends on the iOS version you are using.
On iOS4 and iOS 5 the thumbnail is already correctly rotated, so you can initialize your UIImage without specifying any rotation parameters.
However for the fullScreenImage, the behavior is different for each iOS version. On iOS 5 the image is already rotated on iOS 4 not.

So on iOS4 you should use:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
                                                     scale:[defaultRep scale] orientation:(UIImageOrientation)[defaultRep orientation]];

On iOS5 the following code should work correctly:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];

Cheers,

Hendrik

梦里梦着梦中梦 2025-01-02 05:47:40

试试这个代码:-

 UIImage* img = [UIImage imageWithCGImage:asset.thumbnail];
 img = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationUp];

这可能对你有帮助。

Try this code:-

 UIImage* img = [UIImage imageWithCGImage:asset.thumbnail];
 img = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationUp];

This may help you.

荭秂 2025-01-02 05:47:40

我的经验仅限于 IOS 5.x,但我可以告诉你缩略图和全屏图像的方向正确。这是垂直拍摄时水平的全分辨率图像。我的解决方案是使用我从这里获得的 uiimage 上的类别:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967&start=0

它在 UIImage 上提供了一个很好的旋转方法 像这样:

        UIImage *tmp = [UIImage imageWithCGImage:startingFullResolutionImage];
        startingFullResolutionImage = [[tmp imageRotatedByDegrees:-90.0f] CGImage];

My experience is limited to IOS 5.x but I can tell you that the thumbnail and fullscreen images are oriented properly. It's the fullresolutionimage that's horizontal when shot vertically. My solution is to use a category on uiimage that I got from here:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967&start=0

It provides a nice rotating method on a UIImage like this:

        UIImage *tmp = [UIImage imageWithCGImage:startingFullResolutionImage];
        startingFullResolutionImage = [[tmp imageRotatedByDegrees:-90.0f] CGImage];
月竹挽风 2025-01-02 05:47:40

对于fullResolutionImage,我想提供如下解决方案,

ALAssetRepresentation *rep = [asset defaultRepresentation];

// First, write orientation to UIImage, i.e., EXIF message.
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:(UIImageOrientation)rep.orientation];
// Second, fix orientation, and drop out EXIF
if (image.imageOrientation != UIImageOrientationUp) {
   UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
   [image drawInRect:(CGRect){0, 0, image.size}];
   UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   image = normalizedImage;
}
// Third, compression
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

imageData就是你想要的,只需将其上传到你的照片服务器即可。

顺便说一句,如果您认为 EXIF 有用,您可以根据需要将其补充到 normalizedImage 中。

For fullResolutionImage, I'd like to provide a solution as follows,

ALAssetRepresentation *rep = [asset defaultRepresentation];

// First, write orientation to UIImage, i.e., EXIF message.
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:(UIImageOrientation)rep.orientation];
// Second, fix orientation, and drop out EXIF
if (image.imageOrientation != UIImageOrientationUp) {
   UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
   [image drawInRect:(CGRect){0, 0, image.size}];
   UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   image = normalizedImage;
}
// Third, compression
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

imageData is what you want, and just upload it to your photo server.

By the way, if you think EXIF is useful, you can complement it to normalizedImage as you wish.

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