上传后iOS iOS UIImagePickerController结果图像方向
我正在iOS 3.1.3 iPhone上testing我的iPhone应用程序。 我使用UIImagePickerController
select/捕捉图像:
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:@"http://example.com/myscript.php"]]; [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(在拍摄照片之后)中显示的那样,但是在服务器上查看则不然。
UIImage有一个属性imageOrientation
,它指示UIImageView和其他UIImage使用者旋转原始图像数据。 有一个很好的机会,这个标志被保存到上传的JPEG图像的EXIF数据,但你用来查看它的程序不符合该标志。
要上传UIImage以正确显示,可以使用如下所示的类别:
的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
我想出了一个更简单的:
- (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; }
顺便说一句:@Anomie的代码没有考虑到scale
,所以不适用于2张图片。
这里是@ an0的答案的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; }
另外在一个更一般的function:
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 }
Swift 3.1的解决scheme,用于从摄像头捕获图像时的方向问题。
我已经更新了由杰克和金属心给出的解决scheme
UIImage扩展
//MARK:- Image Orientation fix extension UIImage { func fixOrientation() -> UIImage { // No-op if the orientation is already correct if ( self.imageOrientation == UIImageOrientation.up ) { 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. var transform: CGAffineTransform = CGAffineTransform.identity if ( self.imageOrientation == UIImageOrientation.down || self.imageOrientation == UIImageOrientation.downMirrored ) { transform = transform.translatedBy(x: self.size.width, y: self.size.height) transform = transform.rotated(by: CGFloat(Double.pi)) } if ( self.imageOrientation == UIImageOrientation.left || self.imageOrientation == UIImageOrientation.leftMirrored ) { transform = transform.translatedBy(x: self.size.width, y: 0) transform = transform.rotated(by: CGFloat(Double.pi / 2.0)) } if ( self.imageOrientation == UIImageOrientation.right || self.imageOrientation == UIImageOrientation.rightMirrored ) { transform = transform.translatedBy(x: 0, y: self.size.height); transform = transform.rotated(by: CGFloat(-Double.pi / 2.0)); } if ( self.imageOrientation == UIImageOrientation.upMirrored || self.imageOrientation == UIImageOrientation.downMirrored ) { transform = transform.translatedBy(x: self.size.width, y: 0) transform = transform.scaledBy(x: -1, y: 1) } if ( self.imageOrientation == UIImageOrientation.leftMirrored || self.imageOrientation == UIImageOrientation.rightMirrored ) { transform = transform.translatedBy(x: self.size.height, y: 0); transform = transform.scaledBy(x: -1, y: 1); } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. let ctx: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0, space: self.cgImage!.colorSpace!, bitmapInfo: self.cgImage!.bitmapInfo.rawValue)!; ctx.concatenate(transform) if ( self.imageOrientation == UIImageOrientation.left || self.imageOrientation == UIImageOrientation.leftMirrored || self.imageOrientation == UIImageOrientation.right || self.imageOrientation == UIImageOrientation.rightMirrored ) { ctx.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.height,height: self.size.width)) } else { ctx.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.width,height: self.size.height)) } // And now we just create a new UIImage from the drawing context and return it return UIImage(cgImage: ctx.makeImage()!) } }
Swift 2.0
//MARK:- Image Orientation fix extension UIImage { func fixOrientation() -> UIImage { // No-op if the orientation is already correct if ( self.imageOrientation == UIImageOrientation.Up ) { 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. var transform: CGAffineTransform = CGAffineTransformIdentity if ( self.imageOrientation == UIImageOrientation.Down || self.imageOrientation == UIImageOrientation.DownMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height) transform = CGAffineTransformRotate(transform, CGFloat(M_PI)) } if ( self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) } if ( self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored ) { transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)); } if ( self.imageOrientation == UIImageOrientation.UpMirrored || self.imageOrientation == UIImageOrientation.DownMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformScale(transform, -1, 1) } if ( self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.RightMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. let ctx: CGContextRef = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage).rawValue)!; CGContextConcatCTM(ctx, transform) if ( self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored ) { CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage) } else { CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage) } // And now we just create a new UIImage from the drawing context and return it return UIImage(CGImage: CGBitmapContextCreateImage(ctx)!) } }
在你的代码中使用这个UIImage扩展:
让fixOrientationImage = chosenImage.fixOrientation()
把它放在像这样的图像选取器的委托方法中
Swift 3.1
//MARK: Image Picker Delegates func imagePickerController( _ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){ let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage profileImg.contentMode = .scaleAspectFill let fixOrientationImage=chosenImage.fixOrientation() profileImg.image = fixOrientationImage dismiss(animated: true, completion: nil) }
Swift 2.0
//MARK: Image Picker Delegates func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage profileImg.contentMode = .ScaleAspectFill **//Fix the image orientation** let fixOrientationImage=chosenImage.fixOrientation() profileImg.image = fixOrientationImage dismissViewControllerAnimated(true, completion: nil) }
迅速;)
更新SWIFT 3.0:D
func sFunc_imageFixOrientation(img:UIImage) -> UIImage { // No-op if the orientation is already correct if (img.imageOrientation == UIImageOrientation.up) { return img; } // 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. var transform:CGAffineTransform = CGAffineTransform.identity if (img.imageOrientation == UIImageOrientation.down || img.imageOrientation == UIImageOrientation.downMirrored) { transform = transform.translatedBy(x: img.size.width, y: img.size.height) transform = transform.rotated(by: CGFloat(M_PI)) } if (img.imageOrientation == UIImageOrientation.left || img.imageOrientation == UIImageOrientation.leftMirrored) { transform = transform.translatedBy(x: img.size.width, y: 0) transform = transform.rotated(by: CGFloat(M_PI_2)) } if (img.imageOrientation == UIImageOrientation.right || img.imageOrientation == UIImageOrientation.rightMirrored) { transform = transform.translatedBy(x: 0, y: img.size.height); transform = transform.rotated(by: CGFloat(-M_PI_2)); } if (img.imageOrientation == UIImageOrientation.upMirrored || img.imageOrientation == UIImageOrientation.downMirrored) { transform = transform.translatedBy(x: img.size.width, y: 0) transform = transform.scaledBy(x: -1, y: 1) } if (img.imageOrientation == UIImageOrientation.leftMirrored || img.imageOrientation == UIImageOrientation.rightMirrored) { transform = transform.translatedBy(x: img.size.height, y: 0); transform = transform.scaledBy(x: -1, y: 1); } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. let ctx:CGContext = CGContext(data: nil, width: Int(img.size.width), height: Int(img.size.height), bitsPerComponent: img.cgImage!.bitsPerComponent, bytesPerRow: 0, space: img.cgImage!.colorSpace!, bitmapInfo: img.cgImage!.bitmapInfo.rawValue)! ctx.concatenate(transform) if (img.imageOrientation == UIImageOrientation.left || img.imageOrientation == UIImageOrientation.leftMirrored || img.imageOrientation == UIImageOrientation.right || img.imageOrientation == UIImageOrientation.rightMirrored ) { ctx.draw(img.cgImage!, in: CGRect(x:0,y:0,width:img.size.height,height:img.size.width)) } else { ctx.draw(img.cgImage!, in: CGRect(x:0,y:0,width:img.size.width,height:img.size.height)) } // And now we just create a new UIImage from the drawing context let cgimg:CGImage = ctx.makeImage()! let imgEnd:UIImage = UIImage(cgImage: cgimg) return imgEnd }
我在devise可以拍摄照片的应用程序时使用了这个页面,并发现以下方法可以纠正方向,并且比以前的答案使用更less的内存和处理器:
CGImageRef cgRef = image.CGImage; image = [[UIImage alloc] initWithCGImage:cgRef scale:1.0 orientation:UIImageOrientationUp];
这基本上只是用新的方向重新包装实际的图像数据。 我正在使用@ an0的代码,但是它会在内存中创build一个新的映像,这可能会导致您从相机中获取的3264×2448图像。
如果启用编辑,则编辑后的图像(与原始图像相反)将按预期方向定向:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.allowsEditing = YES; // set delegate and present controller - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *photo = [info valueForKey:UIImagePickerControllerEditedImage]; // do whatever }
启用编辑function允许用户在点击“使用照片”之前调整图像大小并移动图像
这是我find了解决方向问题
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 *initialImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *data = UIImagePNGRepresentation(self.initialImage); initialImage = [UIImage imageWithCGImage:[UIImage imageWithData:data].CGImage scale:initialImage.scale orientation:self.initialImage.imageOrientation];
这是基于@Anomie接受的答案在Swift 2中的UIImage扩展。 它使用更清晰的开关箱。 它还考虑了CGBitmapContextCreateImage()
返回的可选值。
extension UIImage { func rotateImageByOrientation() -> UIImage { // No-op if the orientation is already correct guard self.imageOrientation != .Up else { 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. var transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case .Down, .DownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height) transform = CGAffineTransformRotate(transform, CGFloat(M_PI)) case .Left, .LeftMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) case .Right, .RightMirrored: transform = CGAffineTransformTranslate(transform, 0, self.size.height) transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)) default: break } switch (self.imageOrientation) { case .UpMirrored, .DownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformScale(transform, -1, 1) case .LeftMirrored, .RightMirrored: transform = CGAffineTransformTranslate(transform, self.size.height, 0) transform = CGAffineTransformScale(transform, -1, 1) default: break } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. let ctx = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage).rawValue) CGContextConcatCTM(ctx, transform) switch (self.imageOrientation) { case .Left, .LeftMirrored, .Right, .RightMirrored: CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage) default: CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage) } // And now we just create a new UIImage from the drawing context if let cgImage = CGBitmapContextCreateImage(ctx) { return UIImage(CGImage: cgImage) } else { return self } } }
这里是swift的UIImage扩展:
extension UIImage { func fixOrientation() -> UIImage { // No-op if the orientation is already correct if ( self.imageOrientation == UIImageOrientation.Up ) { 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. var transform: CGAffineTransform = CGAffineTransformIdentity if ( self.imageOrientation == UIImageOrientation.Down || self.imageOrientation == UIImageOrientation.DownMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height) transform = CGAffineTransformRotate(transform, CGFloat(M_PI)) } if ( self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) } if ( self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored ) { transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)); } if ( self.imageOrientation == UIImageOrientation.UpMirrored || self.imageOrientation == UIImageOrientation.DownMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformScale(transform, -1, 1) } if ( self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.RightMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. var ctx: CGContextRef = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage)); CGContextConcatCTM(ctx, transform) if ( self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored ) { CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage) } else { CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage) } // And now we just create a new UIImage from the drawing context and return it return UIImage(CGImage: CGBitmapContextCreateImage(ctx))! } }
基于MetalHeart2003的早期工作
更新Swift 3.1基于Sourabh Sharma
的回答,代码清理。
extension UIImage { func fixedOrientation() -> UIImage { if imageOrientation == .up { return self } var transform:CGAffineTransform = .identity switch imageOrientation { case .down, .downMirrored: transform = transform.translatedBy(x: size.width, y: size.height).rotated(by: .pi) case .left, .leftMirrored: transform = transform.translatedBy(x: size.width, y: 0).rotated(by: .pi/2) case .right, .rightMirrored: transform = transform.translatedBy(x: 0, y: size.height).rotated(by: -.pi/2) default: break } switch imageOrientation { case .upMirrored, .downMirrored: transform = transform.translatedBy(x: size.width, y: 0).scaledBy(x: -1, y: 1) case .leftMirrored, .rightMirrored: transform = transform.translatedBy(x: size.height, y: 0).scaledBy(x: -1, y: 1) default: break } let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage!.bitsPerComponent, bytesPerRow: 0, space: cgImage!.colorSpace!, bitmapInfo: cgImage!.bitmapInfo.rawValue)! ctx.concatenate(transform) switch imageOrientation { case .left, .leftMirrored, .right, .rightMirrored: ctx.draw(cgImage!, in: CGRect(x: 0, y: 0, width: size.height,height: size.width)) default: ctx.draw(cgImage!, in: CGRect(x: 0, y: 0, width: size.width,height: size.height)) } return UIImage(cgImage: ctx.makeImage()!) } }
Picker委托方法的例子:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { guard let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return } let fixedImage = originalImage.fixedOrientation() // do your work }
如果我明白了,你想要做的就是无视UIImage的方向? 如果是这样,那么你可以这样做: –
//image is your original image image = [UIImage imageWithCGImage:[image CGImage] scale:[image scale] orientation: UIImageOrientationUp];
或在斯威夫特: –
image = UIImage(CGImage: image.CGImage!, scale: image.scale, orientation:.Up)
它解决了我的种植问题..希望,这是你正在寻找..
基于@ jake1981的Swift 3版本已经从@ MetalHeart2003中获得了
extension UIImage { func fixOrientation() -> UIImage { // No-op if the orientation is already correct if ( self.imageOrientation == UIImageOrientation.up ) { 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. var transform: CGAffineTransform = CGAffineTransform.identity if ( self.imageOrientation == UIImageOrientation.down || self.imageOrientation == UIImageOrientation.downMirrored ) { transform = transform.translatedBy(x: self.size.width, y: self.size.height) transform = transform.rotated(by: CGFloat(M_PI)) } if ( self.imageOrientation == UIImageOrientation.left || self.imageOrientation == UIImageOrientation.leftMirrored ) { transform = transform.translatedBy(x: self.size.width, y: 0) transform = transform.rotated(by: CGFloat(M_PI_2)) } if ( self.imageOrientation == UIImageOrientation.right || self.imageOrientation == UIImageOrientation.rightMirrored ) { transform = transform.translatedBy(x: 0, y: self.size.height); transform = transform.rotated(by: CGFloat(-M_PI_2)); } if ( self.imageOrientation == UIImageOrientation.upMirrored || self.imageOrientation == UIImageOrientation.downMirrored ) { transform = transform.translatedBy(x: self.size.width, y: 0) transform = transform.scaledBy(x: -1, y: 1) } if ( self.imageOrientation == UIImageOrientation.leftMirrored || self.imageOrientation == UIImageOrientation.rightMirrored ) { transform = transform.translatedBy(x: self.size.height, y: 0); transform = transform.scaledBy(x: -1, y: 1); } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. let ctx: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0, space: self.cgImage!.colorSpace!, bitmapInfo: self.cgImage!.bitmapInfo.rawValue)! ctx.concatenate(transform) if ( self.imageOrientation == UIImageOrientation.left || self.imageOrientation == UIImageOrientation.leftMirrored || self.imageOrientation == UIImageOrientation.right || self.imageOrientation == UIImageOrientation.rightMirrored ) { ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: self.size.height, height: self.size.width)) } else { ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)) } // And now we just create a new UIImage from the drawing context and return it return UIImage(cgImage: ctx.makeImage()!) } }
I have experienced this issue with images taken from camera or saved in camera roll which are taken from camera. Images downloaded in photo library from safari browser does not rotate when uploaded.
I was able to solve this issue by making the image data as JPEG before uploading.
let image = info[UIImagePickerControllerOriginalImage] as! UIImage let data = UIImageJPEGRepresentation(image, 1.0)
We can now use the data for uploading and the image will not get rotated after upload.
希望这会工作。
- UILabel UITextField UITextView
- 为什么使用id,当我们可以只使用NSObject?
- 当他们说一个NSManagedObjectContext被创build它的线程或队列拥有时,苹果是什么意思?
- NSLocale currentLocale总是返回“en_US”而不是用户当前的语言
- 在objective-c / cocoa中抛出一个exception
- 我怎么能从一个UINavigationControllerpopup一个视图,并用另一个操作中的另一个replace它?
- NSTimeZone:localTimeZone和systemTimeZone有什么区别?
- 现在有没有办法使drawRect工作?
- UIWebView旋转后调整文本大小:寻找神奇的bug或我的愚蠢的解释