【问题标题】:CGImage orientation issues when retrieved from ALAssset从 ALAssset 检索时的 CGImage 方向问题
【发布时间】:2011-06-10 21:25:36
【问题描述】:

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

这是我的代码的 sn-p:

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

【问题讨论】:

    标签: iphone xcode uiimage cgimage alasset


    【解决方案1】:

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

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

    试试

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

    【讨论】:

      【解决方案2】:

      您可以将图像重绘到图像上下文中。然后从上下文中获取图像数据,并根据您的需要,发送数据或存储文件以供以后发送。

      UIGraphicsBeginImageContext(largeImage.size);
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextRotateCTM(context, M_PI / 2); // Or negative M_PI depending on the image's orientation
      largeImage.imageOrientation = UIImageOrientationLeft;
      [largeImage drawAtPoint:CGPointZero];
      NSData *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      

      您可能需要“正确”的图像方向,具体取决于您的肖像照片的旋转方式,但这只是进行一次测试的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-23
        • 1970-01-01
        • 2015-08-27
        • 1970-01-01
        • 1970-01-01
        • 2015-04-05
        • 1970-01-01
        相关资源
        最近更新 更多