UIImagePickerController返回错误的图像方向
我正在使用UIImagePickerController捕获图像并存储它。但是,当我尝试重新缩放它时,从该图像中获取的方向值不正确。当我举着手机拍照时,它会给我一个向左的方向。有没有人经历过这个问题 UIImagePickerController字典显示以下信息:UIImagePickerController返回错误的图像方向,uiimage,orientation,uiimagepickercontroller,scale,Uiimage,Orientation,Uiimagepickercontroller,Scale,我正在使用UIImagePickerController捕获图像并存储它。但是,当我尝试重新缩放它时,从该图像中获取的方向值不正确。当我举着手机拍照时,它会给我一个向左的方向。有没有人经历过这个问题 UIImagePickerController字典显示以下信息: { UIImagePickerControllerMediaMetadata = { DPIHeight = 72; DPIWidth = 72; Orientation
{
UIImagePickerControllerMediaMetadata = {
DPIHeight = 72;
DPIWidth = 72;
Orientation = 3;
"{Exif}" = {
ApertureValue = "2.970853654340484";
ColorSpace = 1;
DateTimeDigitized = "2011:02:14 10:26:17";
DateTimeOriginal = "2011:02:14 10:26:17";
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.06666666666666667";
FNumber = "2.8";
Flash = 32;
FocalLength = "3.85";
ISOSpeedRatings = (
125
);
MeteringMode = 1;
PixelXDimension = 2048;
PixelYDimension = 1536;
SceneType = 1;
SensingMethod = 2;
Sharpness = 1;
ShutterSpeedValue = "3.910431673351467";
SubjectArea = (
1023,
767,
614,
614
);
WhiteBalance = 0;
};
"{TIFF}" = {
DateTime = "2011:02:14 10:26:17";
Make = Apple;
Model = "iPhone 3GS";
Software = "4.2.1";
XResolution = 72;
YResolution = 72;
};
};
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "<UIImage: 0x40efb50>";
}
我刚开始在我自己的应用程序中解决这个问题 我使用Trevor Harmon制作的UIImage类别来调整图像大小和固定其方向 然后您可以在
-imagePickerController:didFinishPickingMediaWithInfo:
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *resized = [pickedImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:pickedImage.size interpolationQuality:kCGInterpolationHigh];
UIImage*pickedImage=[info objectForKey:UIImagePickerControllerEditedImage];
UIImage*resized=[pickedImage ResizedImage WithContentMode:UIViewContentModeScaleAspectFit界限:pickedImage.size插值质量:kCGInterpolationHigh];
这为我解决了问题。调整大小的图像在视觉上方向正确,并且imageOrientation属性报告UIImageOrientationUp
有几个版本的缩放/调整大小/裁剪代码;我使用了Trevor的,因为它看起来很干净,还包括一些我想稍后使用的其他UIImage操纵器 这是我发现的解决方向问题的方法;为我工作
UIImage *initialImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data = UIImagePNGRepresentation(self.initialImage);
UIImage *tempImage = [UIImage imageWithData:data];
UIImage *fixedOrientationImage = [UIImage imageWithCGImage:tempImage.CGImage
scale:initialImage.scale
orientation:self.initialImage.imageOrientation];
initialImage = fixedOrientationImage;
我使用以下代码,这些代码放在一个单独的图像实用程序对象文件中,该文件包含一系列其他UIImage编辑方法:
+ (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize
{
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor) {
scaleFactor = widthFactor; // scale to fit height
}
else {
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor < heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
// In the right or left cases, we need to switch scaledWidth and scaledHeight,
// and also the thumbnail point
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap, M_PI_2); // + 90 degrees
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap, -M_PI_2); // - 90 degrees
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, -M_PI); // - 180 degrees
}
CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
下面是一个可以有效解决问题的Swift片段:
let orientedImage = UIImage(CGImage: initialImage.CGImage, scale: 1, orientation: initialImage.imageOrientation)!
在iOS 7中,我需要依赖于UIImage.imageOrientation的代码来纠正不同的方向。现在,在iOS 8.2中,当我通过UIImagePickerController从相册中拾取旧的测试图像时,所有图像的方向都是UIImageOrientationUp。当我拍照时(UIImagePickerControllerSourceTypeCamera),无论设备方向如何,这些图像也将始终向上。 因此,在这些iOS版本之间,显然有一个修复程序,UIImagePickerController已经在必要时旋转图像。
您甚至可以注意到,当相册图像显示时:在一瞬间,它们将以原始方向显示,然后再以新的向上方向显示。唯一对我有效的方法是再次重新渲染图像,从而强制正确的方向
if (photo.imageOrientation != .up) {
UIGraphicsBeginImageContextWithOptions(photo.size, false, 1.0);
let rect = CGRect(x: 0, y: 0, width: photo.size.width, height: photo.size.height);
photo.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
photo = newImage;
}
这似乎不适用于符合ARC的代码。它失败了,错误是:“没有可见的@interface for'UIImage'声明选择器'resizedImageWithContentMode:bounds:interpolationQuality:'”您可以查看由Marc Charbonneau在GitHub上维护的这段代码的最新分支。即使使用Marc的fork,这仍然不能修复方向。它将始终返回UIImageOrientationUp。所以这只适用于方向UIImageOrientationUp,而不适用于其他方向。收到内存警告。谢谢,这为我修复了它!不幸的是,它不起作用。新图像完全相同,方向错误
let orientedImage = UIImage(CGImage: initialImage.CGImage, scale: 1, orientation: initialImage.imageOrientation)!
if (photo.imageOrientation != .up) {
UIGraphicsBeginImageContextWithOptions(photo.size, false, 1.0);
let rect = CGRect(x: 0, y: 0, width: photo.size.width, height: photo.size.height);
photo.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
photo = newImage;
}