[英]After getting image from UIImagePickerController, UIImageView rotates image for iPhone 5
I am using UIImagePickerController to capture image using camera. 我正在使用UIImagePickerController来使用相机捕获图像。 Supported orientation for my app is Portrait.
我的应用程序支持的方向是Portrait。 I am seeing strange behavior for iPhone 5. I am using Xcode 7 and swift 2.0.
我看到iPhone 5的奇怪行为。我正在使用Xcode 7和swift 2.0。 iPhone 5 OS version is 8.4.
iPhone 5 OS版本是8.4。 Deployment Target is 8.0 for my app.
我的应用程序的部署目标是8.0。
Issues are. 问题是。 1. For iPhone 5, after capturing image, image is shown in respective mode in which image is captured.
1.对于iPhone 5,在捕获图像后,图像以相应的模式显示,其中捕获图像。 But After I press 'Use Photo' standard option and when image is displayed on UIImageView, image is automatically rotated to left.
但在按下“使用照片”标准选项后,当图像显示在UIImageView上时,图像会自动旋转到左侧。 Don't know why.
不知道为什么。 If I choose image from photo library, image is not rotated.
如果我从照片库中选择图像,则图像不会旋转。 I don't want image to be rotated.
我不希望图像被旋转。 I seen similar post with better explanation and actual image but is not answered.
我看到类似的帖子有更好的解释和实际图像,但没有回答。 UIImageView rotates image with retina 4 iPhone simulator but not retina 3.5/regular simulator I tried almost all swift solution from this post : iOS UIImagePickerController result image orientation after upload and other post too but nothing seems to be working.
UIImageView旋转图像与视网膜4 iPhone模拟器,但没有视网膜3.5 /常规模拟器我尝试了几乎所有快速解决方案从这篇文章: iOS UIImagePickerController结果图像方向上传和其他发布后,似乎没有什么工作。 I used shouldAutorotate(), sFunc_imageFixOrientation(), and adding extension from this post.
我使用了shouldAutorotate(),sFunc_imageFixOrientation(),并在此帖子中添加了扩展名。
Here is my code: 这是我的代码:
func openCamera() { func openCamera(){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
dispatch_async(dispatch_get_main_queue(), {
let imagePicker = UIImagePickerController();
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.allowsEditing = false;
imagePicker.delegate = self;
imagePicker.modalPresentationStyle = .FormSheet
self.presentViewController(imagePicker, animated: true, completion: nil);
});
}
}
func openGallary() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
profileImage?.image = image
self.dismissViewControllerAnimated(true, completion: nil);
}
This is what I observed. 这是我观察到的。 When you capture image using camera, image is rotated counterclockwise by 90 degree and image orientation is set to UIImageOrientation.Up so code from function sFunc_imageFixOrientation iOS UIImagePickerController result image orientation after upload will not work.
使用相机捕获图像时,图像逆时针旋转90度,图像方向设置为UIImageOrientation.Up,因此上传后的函数sFunc_imageFixOrientation iOS UIImagePickerController结果图像方向的代码将无效。 If you select image from photo library, then image will be displayed as it is.
如果从照片库中选择图像,则图像将按原样显示。
Modified function: 修改功能:
func sFunc_imageFixOrientation(img:UIImage) -> UIImage {
// 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
// Rotate image clockwise by 90 degree
if (img.imageOrientation == UIImageOrientation.Up) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.Down
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
}
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
}
if (img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.UpMirrored
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformScale(transform, -1, 1)
}
if (img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, img.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(img.size.width), Int(img.size.height),
CGImageGetBitsPerComponent(img.CGImage), 0,
CGImageGetColorSpace(img.CGImage),
CGImageGetBitmapInfo(img.CGImage).rawValue)!;
CGContextConcatCTM(ctx, transform)
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored
|| img.imageOrientation == UIImageOrientation.Up
) {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage)
} else {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage)
}
// And now we just create a new UIImage from the drawing context
let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)!
let imgEnd:UIImage = UIImage(CGImage: cgimg)
return imgEnd
}
}
Call this function only if image is captured from Camera. 仅在从相机捕获图像时才调用此功能。 I know my answer will not be perfect but you can definitely try if nothing is working out for you.
我知道我的答案不会很完美,但如果没有什么能帮到你的话,你绝对可以尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.