上传后IOS UIImagePickerController结果图像定向

上传后IOS UIImagePickerController结果图像定向

我正在iOS3.1.3 iPhone上测试我的iPhone应用程序。我使用UIImagePickerController:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];[imagePicker setSourceType:
UIImagePickerControllerSourceTypeCamera];[imagePicker setDelegate:self];[self.navigationController presentModalViewController:
imagePicker animated:YES];[imagePicker release];- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *)info {
    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    imageView.image = self.image;
    [self.navigationController dismissModalViewControllerAnimated:YES];
    submitButton.enabled = YES;}

然后在某个时候使用ASI类将其发送到我的Web服务器:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@" 
[request setDelegate:self];[request setStringEncoding:NSUTF8StringEncoding];[request setShouldContinueWhenAppEntersBackground:YES];
//other post keys/values[request setFile:UIImageJPEGRepresentation(self.image, 100.0f) withFileName:[NSString stringWithFormat:@"%d.jpg",
 [[NSDate date] timeIntervalSinceNow]] andContentType:@"image/jpg" forKey:@"imageFile"];[request startAsynchronous];

问题是:当我和iPhone合影时,图像会上传到服务器上,并按您的预期进行查看。当拍照时,拿着手机的肖像,图像上传和查看,因为它已经旋转了90度。

我的应用程序被设置为只工作在纵向模式(倒置和常规)。

如何使图片上传后始终显示正确的方向?

在UIImageView中显示的图像似乎是正确的(在拍照后直接显示),但服务器上的查看则不然。


Helenr
浏览 582回答 3
3回答

慕姐4208626

我想出了一个简单得多的方法:- (UIImage *)normalizedImage {     if (self.imageOrientation == UIImageOrientationUp) return self;      UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);     [self drawInRect:(CGRect){0, 0, self.size}];     UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();     return normalizedImage;}顺便说一句:@失范的代码不需要scale考虑到,所以不会工作2倍的图像。

料青山看我应如是

以下是@an 0的答案的一个SWIFT版本:func normalizedImage() -> UIImage {   if (self.imageOrientation == UIImageOrientation.Up) {        return self;   }   UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);   let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)   self.drawInRect(rect)   let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()   UIGraphicsEndImageContext();   return normalizedImage;}还有一个更一般的职能:func fixOrientation(img:UIImage) -> UIImage {   if (img.imageOrientation == UIImageOrientation.Up) {        return img;   }   UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale);   let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)   img.drawInRect(rect)   let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()   UIGraphicsEndImageContext();   return normalizedImage;}SWIFT 3版本:func fixOrientation(img: UIImage) -> UIImage {     if (img.imageOrientation == .up) {         return img    }     UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale)     let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)     img.draw(in: rect)     let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()!     UIGraphicsEndImageContext()     return normalizedImage}

慕丝7291255

A UIImage有财产imageOrientation,它指示UIImageView和其他UIImage使用者旋转原始图像数据。这个标志很有可能被保存到上传的jpeg图像中的EXIF数据,但是您用来查看它的程序没有遵守该标志。旋转UIImage若要在上载时正确显示,可以使用以下类别:UIImage+FixOrientOriented.h@interface UIImage (fixOrientation)- (UIImage *)fixOrientation;@endUIImage+FixOrientOriented.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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS