内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
我正在iOS3.1.3测试程序。我使用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:@"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中正确显示,但服务器上却不能。
解决SWIFT 3.1的图片方向问题。
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) }