我正在上传从iPhone拍摄的横向和纵向模式的图像。
横向模式的图像上传正常,但是纵向模式的图像上传存在问题,它们会旋转90度。
另外,其他纵向模式的图像(非iPhone拍摄)可以正常工作。
有任何想法为什么会出现这种情况吗?
我正在上传从iPhone拍摄的横向和纵向模式的图像。
横向模式的图像上传正常,但是纵向模式的图像上传存在问题,它们会旋转90度。
另外,其他纵向模式的图像(非iPhone拍摄)可以正常工作。
有任何想法为什么会出现这种情况吗?
在您的代理中:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
当您从“info”字典中获取键为“UIImagePickerControllerOriginalImage
”的UIImage
后,可以通过imageOrientation属性查看图像方向。如果不符合您的要求,请在上传之前旋转图像。
imageOrientation
The orientation of the receiver’s image. (read-only)
@property(nonatomic, readonly) UIImageOrientation imageOrientation
Discussion
Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the “up” orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata. For a list of possible values for this property, see “UIImageOrientation.”
Availability
Available in iOS 2.0 and later.
Declared In
UIImage.h
UIImagePickerControllerDelegate协议参考资料
第二种选择:
允许用户编辑您的图像并获取“UIImagePickerControllerEditedImage
”的图像;
将您的UIImagePickerController
“allowsEditing”属性设置为Yes
。
在委托中,只需从“info”字典获取UIImage
的“UIImagePickerControllerEditedImage
”键即可。
祝好运。
我曾经苦恼于这个问题很长时间,因为我正在处理一个需要实际旋转图片的项目,也就是说我需要重新排列像素才能上传它。
首先你需要确定图片的方向,然后去掉那些讨厌的元数据,最后再旋转图片。
所以将以下代码放到didFinishPickingMediaWithInfo
函数中:
UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if ([info objectForKey:@"UIImagePickerControllerMediaMetadata"]) {
//Rotate based on orientation
switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) {
case 3:
//Rotate image to the left twice.
img = [UIImage imageWithCGImage:[img CGImage]]; //Strip off that pesky meta data!
img = [rotateImage rotateImage:[rotateImage rotateImage:img withRotationType:rotateLeft] withRotationType:rotateLeft];
break;
case 6:
img = [UIImage imageWithCGImage:[img CGImage]];
img = [rotateImage rotateImage:img withRotationType:rotateRight];
break;
case 8:
img = [UIImage imageWithCGImage:[img CGImage]];
img = [rotateImage rotateImage:img withRotationType:rotateLeft];
break;
default:
break;
}
}
这里是调整大小函数:
+(UIImage*)rotateImage:(UIImage*)image withRotationType:(rotationType)rotation{
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap;
bitmap = CGBitmapContextCreate(NULL, image.size.height, image.size.width, CGImageGetBitsPerComponent(imageRef), 4 * image.size.height/*CGImageGetBytesPerRow(imageRef)*/, colorSpaceInfo, alphaInfo);
CGColorSpaceRelease(colorSpaceInfo);
if (rotation == rotateLeft) {
CGContextTranslateCTM (bitmap, image.size.height, 0);
CGContextRotateCTM (bitmap, radians(90));
}
else{
CGContextTranslateCTM (bitmap, 0, image.size.width);
CGContextRotateCTM (bitmap, radians(-90));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
CGContextRelease(bitmap);
return result;
}
img
变量现在包含一个正确旋转的图像。
好的,更干净的版本应该是:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage];
img = [UIImage imageWithCGImage:[img CGImage]];
UIImageOrientation requiredOrientation = UIImageOrientationUp;
switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue])
{
case 3:
requiredOrientation = UIImageOrientationDown;
break;
case 6:
requiredOrientation = UIImageOrientationRight;
break;
case 8:
requiredOrientation = UIImageOrientationLeft;
break;
default:
break;
}
UIImage *portraitImage = [[UIImage alloc] initWithCGImage:img.CGImage scale:1.0 orientation:requiredOrientation];
}