iphone - 从 ALAssset 检索时的 CGImage 方向问题

标签 iphone xcode uiimage cgimage alasset

我刚刚开始学习 iPhone 应用程序开发。我正在尝试使用 ALAsset 类从我的相册中检索照片并将照片上传到我的服务器。然而,在人像模式下拍摄的照片向左旋转90度,而风景照片则正确上传。我究竟做错了什么?在 NSLog 中,我确实看到方向为 3,但照片仍然向左旋转 90 度。任何帮助将不胜感激。

这是我的代码片段:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];

   imagePath = [[documentsDirectory stringByAppendingPathComponent:@"latest_photo.jpg"] copy];
   NSLog(@"imagePath = %@", imagePath);

   ALAssetRepresentation *rep = [myasset defaultRepresentation];
   ALAssetOrientation orientation = [rep orientation];
   NSLog(@"Orientation = %d", orientation);

   CGImageRef iref = [rep fullResolutionImage];

   if (iref) {
      UIImage *largeimage = [UIImage imageWithCGImage:iref scale:1.0 orientation:orientation];
      NSData *webData = UIImageJPEGRepresentation(largeimage,0.5);

      [webData writeToFile:imagePath atomically:YES];

      // Upload the image

最佳答案

对于代码的 Assets 导向部分,而不是

ALAssetRepresentation *rep = [myasset defaultRepresentation];
ALAssetOrientation orientation = [rep orientation];

尝试

ALAssetOrientation orientation = [[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];

关于iphone - 从 ALAssset 检索时的 CGImage 方向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6312231/

相关文章:

iphone - 如何将 OpenGL View 捕获为 UIImage?

ios - Xcode 6 : can't connect any IBOutlet to ViewController

iphone - 使用 ImageIO 在 iOS 上创建渐进式 jpeg 在设备上产生 block 状结果

ios - 带有 NSData initWithData 的 UIImage 为 nil

ios - 简单动画问题 Xcode 7 Swift

iphone - 如何在 iOS 中提取字符串中两个分隔符之间的文本?

ios - UIPageViewController 在我的 UIImage 的底部创建一个边框以及如何摆脱它

iphone - 如何在 Cocos2d 中为场景创建平铺、叠加?

C++ 运算符重载奇怪的类型转换

arrays - 显示核心数据库中 PickerView 的值