我正在使用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
,则图像不会旋转。但这是另一个时代的另一个故事。
不要用UIImagePNGRrepresentation或UIImageJPEGRepresentation保存它。使用ImageIO框架。这样可以使用正确的旋转信息进行保存。
或者,根据你正在做的事情(你没有确切解释你是如何获得这些图像的),通过CGImage可能就足够了。您可以将旋转信息附加到它上,同时将其包装为UIImage。
图像正在旋转,因为您将图像保存为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;
}