iPhone/Xcode: UIImage and UIImageView won't get with the program

时光毁灭记忆、已成空白 提交于 2019-12-04 17:23:15

Ok. I figured it out. Apparently when you save things with PNG representation, and not JPEG representation, the image orientation information is not saved with it. Because of this, every image loaded will default to showing orientation up. So, the easiest way is to do this:

NSData* data = UIImageJPEGRepresentation(image,0.0);
[data writeToFile:path atomically:YES];

instead of this:

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

And, of course, changing all the picture names from .png to .jpg. The 0.0 part of the code above is to control Alpha levels, which is required of jpegs. Hope this helps people in the future!

I assume you're using the Assets Library Framework to get the image. If so, you'll want to get the orientation of the image in your results block, and manipulate the UIImage to adjust. I have something like the following:

ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset)
{
  ALAssetRepresentation* rep = [asset defaultRepresentation];
  _orientation = rep.orientation;

...

}

Aside: What a great type name, huh? Holy moly, does Objective-C need namespaces!

Thanks so much... This code has worked and saved several hours of work for me.

NSData* data = UIImageJPEGRepresentation(image,0.0);
[data writeToFile:path atomically:YES];

in the place of

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