gpt4 book ai didi

swift - Ios swift相机图像选择器旋转问题

转载 作者:可可西里 更新时间:2023-11-01 00:42:15 24 4
gpt4 key购买 nike

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {}

从上面的代码中检索到的图像会将图像旋转 90 度任何解决方案?

最佳答案

我遇到了同样的问题。请检查此 UIImage 扩展:

import UIKit

extension UIImage {

func fixedOrientation() -> UIImage {
// No-op if the orientation is already correct
if (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 (imageOrientation == UIImageOrientation.down
|| imageOrientation == UIImageOrientation.downMirrored) {

transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: CGFloat(M_PI))
}

if (imageOrientation == UIImageOrientation.left
|| imageOrientation == UIImageOrientation.leftMirrored) {

transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.rotated(by: CGFloat(M_PI_2))
}

if (imageOrientation == UIImageOrientation.right
|| imageOrientation == UIImageOrientation.rightMirrored) {

transform = transform.translatedBy(x: 0, y: size.height);
transform = transform.rotated(by: CGFloat(-M_PI_2));
}

if (imageOrientation == UIImageOrientation.upMirrored
|| imageOrientation == UIImageOrientation.downMirrored) {

transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
}

if (imageOrientation == UIImageOrientation.leftMirrored
|| imageOrientation == UIImageOrientation.rightMirrored) {

transform = transform.translatedBy(x: 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(size.width), height: Int(size.height),
bitsPerComponent: cgImage!.bitsPerComponent, bytesPerRow: 0,
space: cgImage!.colorSpace!,
bitmapInfo: cgImage!.bitmapInfo.rawValue)!

ctx.concatenate(transform)


if (imageOrientation == UIImageOrientation.left
|| imageOrientation == UIImageOrientation.leftMirrored
|| imageOrientation == UIImageOrientation.right
|| imageOrientation == UIImageOrientation.rightMirrored
) {


ctx.draw(cgImage!, in: CGRect(x:0,y:0,width:size.height,height:size.width))

} else {
ctx.draw(cgImage!, in: CGRect(x:0,y:0,width:size.width,height: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
}
}

让我知道它是否适合您。 :)

关于swift - Ios swift相机图像选择器旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42346214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com