1

我正在使用 UIImagePickerController 获取 UIImage。得到图像后,我将其设置为 UITableView 的背景。这张照片看起来和我拍的一模一样。但是,如果我将图像保存到我的服务器然后重新加载图像。无论我使用UIImagePNGRepresentation还是保存UIImageJPEGRepresentation,图像都会在保存时旋转(旋转发生在服务器调用之前)。我在这里读到 png 会旋转,但 jpeg 不会。但对我来说,两者都旋转了 90 度。有什么想法为什么 jpeg 在我的情况下不好?

更多的

我从图像选择器中获取原始图像作为 UIImage。然后我保留一个指向 UIImage 的指针。一段时间后,我想保存 UIImage。所以通常我会这样做UIImagePNGRepresentation(myUIImage)。我正在使用 AFNetworking 作为

if ([self.imageDictionaries count]>0)
  for (NSDictionary *imgDic in self.imageDictionaries) {
    [formData appendPartWithFileData:UIImagePNGRepresentation([imgDic objectForKey:@"image"])
    name:[imgDic objectForKey:@"name"]//@"image"
    fileName:[imgDic objectForKey:@"fileName"]//@"image.png"
    mimeType:[imgDic objectForKey:@"mimeType"]//@"image/png"
    ];
  }
}
//[imgDic objectForKey:@"name"] is just a UIImage

imageDictionaries 的其中一项看起来像这样

NSDictionary *background =@{
                                @"image":self.backgroundImage,
                                @"name":@"bkgimg",
                                @"fileName":@"bkgimg.png",
                                @"mimeType":@"image/png"
                                };

另请注意,我的代码工作正常。唯一的问题是图像在旋转。self.backgroundImage是一个指向我从 UIImagePickerController 获得的 UIImage 的指针

UIImage *background = info[UIImagePickerControllerOriginalImage];

如果我使用UIImagePickerControllerEditedImage. 但这是另一回事了。

4

2 回答 2

0

不要使用 UIImagePNGRepresentation 或 UIImageJPEGRepresentation 保存它。使用 ImageIO 框架。这使您可以使用正确的旋转信息进行保存。

或者,根据你在做什么(你没有确切解释你是如何获得这些图像的),通过 CGImage 可能就足够了。您可以在将其包装为 UIImage 时附加旋转信息。

于 2014-09-26T21:48:46.893 回答
0

图像正在旋转,因为您将图像保存为 JPEG,如果您将图像保存为 PNG,则方向不会改变。这是解决方向问题的代码。

- (UIImage *)fixrotation:(UIImage *)image{


if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;

switch (image.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, image.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (image.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                         CGImageGetBitsPerComponent(image.CGImage), 0,
                                         CGImageGetColorSpace(image.CGImage),
                                         CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
        break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img; 
}
于 2015-10-23T11:56:54.417 回答