从 UIImagePickerController 获取图像后,UIImageView 为 iPhone 5 旋转图像
我正在使用 UIImagePickerController 使用相机捕获图像.我的应用程序支持的方向是纵向.我看到 iPhone 5 的奇怪行为.我使用的是 Xcode 7 和 swift 2.0.iPhone 5 操作系统版本为 8.4.我的应用的部署目标是 8.0.
I am using UIImagePickerController to capture image using camera. Supported orientation for my app is Portrait. I am seeing strange behavior for iPhone 5. I am using Xcode 7 and swift 2.0. iPhone 5 OS version is 8.4. Deployment Target is 8.0 for my app.
问题是.1.对于iPhone 5,在拍摄图像后,图像以各自的图像拍摄模式显示.但是当我按下使用照片"标准选项并且在 UIImageView 上显示图像时,图像会自动向左旋转.不知道为什么.如果我从照片库中选择图像,图像不会旋转.我不想旋转图像.我看到类似的帖子有更好的解释和实际图像,但没有得到回答.UIImageView 旋转图像视网膜 4 iPhone 模拟器但不是视网膜 3.5/常规模拟器我尝试了这篇文章中几乎所有的快速解决方案:iOS UIImagePickerController result image上传和其他帖子后的定位,但似乎没有任何效果.我使用了 shouldAutorotate()、sFunc_imageFixOrientation(),并从这篇文章中添加了扩展.
Issues are. 1. For iPhone 5, after capturing image, image is shown in respective mode in which image is captured. But After I press 'Use Photo' standard option and when image is displayed on UIImageView, image is automatically rotated to left. 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. I used shouldAutorotate(), sFunc_imageFixOrientation(), and adding extension from this post.
- 此外,对于两种设备,在按下使用照片"选项后,上传图像大约需要 10 秒.可以更快完成吗.
这是我的代码:
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);
}
这是我观察到的.当您使用相机捕获图像时,图像逆时针旋转 90 度,图像方向设置为 UIImageOrientation.Up 所以来自函数 sFunc_imageFixOrientation iOS UIImagePickerController 上传后的结果图像方向 将不起作用.如果您从照片库中选择图像,则图像将按原样显示.
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. If you select image from photo library, then image will be displayed as it is.
修改功能:
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.