我让用户从相机胶卷中选择要裁剪的照片。我以缩略图的形式显示所有这些图片供用户按下。
我使用UIImage(CGImage: asset.fullScreenImage!.CGImage!, scale: scale, orientation: UIImageOrientation.Up)
确保我的图片与缩略图中的图片保持一致。使用asset.fullResolutionImage!
,这不起作用。
现在出现了奇怪的部分。当我用iPhone相机拍照时,然后前往我的裁剪ViewController,图像旋转90度!我不明白为什么。
如果我离开我的应用程序,请正常使用相机拍照,将其保存到相机胶卷,然后从缩略图列表中选择它,就像我做其他照片一样,效果非常好。
这是什么原因?如何修复它?
编辑:下面发布了两个解决方案。一个在Objective-C中,另一个是在Swift中的翻译副本。
答案 0 :(得分:1)
您可以尝试使用此类别:
它是在Objective-C中构建的,然后您可以使用bridging header将 ObjC 与 Swift 结合使用,或者您可以查看了解如何解决这个问题。
用法示例:
UIImage *image = <image from camera image>
image = [image fixOrientation];
答案 1 :(得分:0)
这个答案是为了补充gontovnik的答案。
我已经采用了他的Objective-C解决方案并将其编写在Swift中。在任何需要的地方放置此功能。只需传递有问题的UIImage。
那就是说,你可能只是让它成为一个UIImage类函数。实际上只需拨打UIImage.fixOrientation(image: imageInQuestion)
即可。
func fixOrientation(image: UIImage) -> UIImage {
// No-op if the orientation is already correct
if (image.imageOrientation == UIImageOrientation.Up) { return image; }
println(image.imageOrientation)
// 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 (image.imageOrientation) {
case .Down, .DownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
break
case .Left, .LeftMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
break
case .Right, .RightMirrored:
transform = CGAffineTransformTranslate(transform, 0, image.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
break
case .Up, .UpMirrored:
break
}
switch (image.imageOrientation) {
case .UpMirrored, .DownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0)
transform = CGAffineTransformScale(transform, -1, 1)
break
case .LeftMirrored, .RightMirrored:
transform = CGAffineTransformTranslate(transform, image.size.height, 0)
transform = CGAffineTransformScale(transform, -1, 1)
break
case .Up, .Down, .Left, .Right:
break
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
var ctx = CGBitmapContextCreate(nil, Int(image.size.width), Int(image.size.height), CGImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColorSpace(image.CGImage), CGImageGetBitmapInfo(image.CGImage))
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
case .Left, .LeftMirrored, .Right, .RightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.height, image.size.width), image.CGImage)
break
default:
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage)
break
}
// And now we just create a new UIImage from the drawing context
var cgimg = CGBitmapContextCreateImage(ctx)
var img = UIImage(CGImage: cgimg)
return img!
}
答案 2 :(得分:0)
Swift 3版本
"Session","QueryString","Application","cookie","Server.Transfer"