【问题标题】:How to rotate image in proper orientation如何以正确的方向旋转图像
【发布时间】:2019-02-14 09:46:19
【问题描述】:

我正在捕获图像并将其存储为 NSData 格式。当我以纵向设备方向拍摄图像时,图像向左旋转 90 度。当我以横向设备方向捕获图像时,我正在以正确的方式获取图像。我尝试以下功能。但我不知道 cgImage 需要采取什么作为 CGImage。

func rotateImageProperly(image:UIImage) -> UIImage {

    let normalImage:UIImage!

    if(image.imageOrientation == .up) {
        normalImage = image
    }else if (image.imageOrientation == .left){
        normalImage = UIImage(cgImage: <#T##CGImage#>, scale: 1.0, orientation: UIImageOrientation.right)
    }


    return normalImage
}

【问题讨论】:

标签: ios swift cgimage


【解决方案1】:

使用以下代码正确旋转图像:

func imageOrientation(_ src:UIImage)->UIImage {
    if src.imageOrientation == UIImageOrientation.up {
        return src
    }
    var transform: CGAffineTransform = CGAffineTransform.identity
    switch src.imageOrientation {
    case UIImageOrientation.down, UIImageOrientation.downMirrored:
        transform = transform.translatedBy(x: src.size.width, y: src.size.height)
        transform = transform.rotated(by: CGFloat(Double.pi))
        break
    case UIImageOrientation.left, UIImageOrientation.leftMirrored:
        transform = transform.translatedBy(x: src.size.width, y: 0)
        transform = transform.rotated(by: CGFloat(Double.pi))
        break
    case UIImageOrientation.right, UIImageOrientation.rightMirrored:
        transform = transform.translatedBy(x: 0, y: src.size.height)
        transform = transform.rotated(by: CGFloat(Double.pi))
        break
    case UIImageOrientation.up, UIImageOrientation.upMirrored:
        break
    }

    switch src.imageOrientation {
    case UIImageOrientation.upMirrored, UIImageOrientation.downMirrored:
        transform.translatedBy(x: src.size.width, y: 0)
        transform.scaledBy(x: -1, y: 1)
        break
    case UIImageOrientation.leftMirrored, UIImageOrientation.rightMirrored:
        transform.translatedBy(x: src.size.height, y: 0)
        transform.scaledBy(x: -1, y: 1)
    case UIImageOrientation.up, UIImageOrientation.down, UIImageOrientation.left, UIImageOrientation.right:
        break
    }

    let ctx:CGContext = CGContext(data: nil, width: Int(src.size.width), height: Int(src.size.height), bitsPerComponent: (src.cgImage)!.bitsPerComponent, bytesPerRow: 0, space: (src.cgImage)!.colorSpace!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!

    ctx.concatenate(transform)

    switch src.imageOrientation {
    case UIImageOrientation.left, UIImageOrientation.leftMirrored, UIImageOrientation.right, UIImageOrientation.rightMirrored:
        ctx.draw(src.cgImage!, in: CGRect(x: 0, y: 0, width: src.size.height, height: src.size.width))
        break
    default:
        ctx.draw(src.cgImage!, in: CGRect(x: 0, y: 0, width: src.size.width, height: src.size.height))
        break
    }

    let cgimg:CGImage = ctx.makeImage()!
    let img:UIImage = UIImage(cgImage: cgimg)

    return img
}

用法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
    var image = info[UIImagePickerControllerOriginalImage] as! UIImage
    image = self.imageOrientation(image)
}

【讨论】:

  • 它不工作的伙伴,当我以纵向模式拍摄图像时,白色的空白图像存储在我的画廊中:(
  • 哦,你能分享更多代码吗?你的问题并没有真正说明主要问题,
猜你喜欢
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
  • 2020-12-31
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多