Orientation does not behave correctly with Photo in ALAsset

假如想象 提交于 2019-12-02 23:03:55

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

Try this code:-

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

This may help you.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!