我正在使用UIImagePickerController
在Objective C应用中拍照。然后用相机拍摄照片或从我在UIImageView
中显示的图库加载。这是我的问题,图片显示旋转90º如果这是纵向设备...
如果我在UIImage
显示UIImageView
的方向,请始终获取UIImageOrientationUp
,因此我无法手动轮换...
if (photo.imageOrientation == UIImageOrientationUp) {
NSLog(@"UP"); //Always get this line. If Uiimage is portrait or landscape
}else if (photo.imageOrientation == UIImageOrientationRight) {
NSLog(@"Right");
}
我正在尝试使用以下功能:
- (UIImage *)fixrotation:(UIImage *)image{
...
}
但图片不会旋转。因为我总是UIImageOrientationUp
..
如何解决这个问题,并在我的UIImageView
?
我的didFinishPickingMediaWithInfo是:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info valueForKey:UIImagePickerControllerOriginalImage];
//I use this image to show in next View
SecondView * controller = [SecondView new];
controller.imageView.image = image;
[self performSegueWithIdentifier:@"segue2" sender:self];
[picker.self dismissModalViewControllerAnimated:YES];
}
然后在ThirdView中将此分配给其他segue之间的UIImageView: //照片是从segue获得的变量 UIImage * imageToDisplay = photo.fixOrientation;
self.imageV.image = imageToDisplay;
self.imageV.contentMode = UIViewContentModeScaleAspectFill;
感谢。
答案 0 :(得分:0)
使用此
的类别<强>的UIImage + fixOrientation.h 强>
@interface UIImage (fixOrientation)
- (UIImage *)fixOrientation;
@end
<强>的UIImage + fixOrientation.m 强>
@implementation UIImage (fixOrientation)
- (UIImage *)fixOrientation {
// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}
switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.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, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.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;
}
@end
答案 1 :(得分:0)
我也面临这个问题。 此代码必须解决问题:
UIImage *initialImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data = UIImagePNGRepresentation(initialImage);
UIImage *tempImage = [UIImage imageWithData:data];
UIImage *fixedOrientationImage = [UIImage imageWithCGImage:tempImage.CGImage
scale:initialImage.scale
orientation:initialImage.imageOrientation];