首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift 3和CGContextDrawImage

Swift 3和CGContextDrawImage
EN

Stack Overflow用户
提问于 2016-09-15 22:41:16
回答 2查看 20.1K关注 0票数 24

我想把这一行翻译成Swift 3当前的语法代码,但似乎有一些问题:

代码语言:javascript
复制
CGContextDrawImage(context, CGRect(x:0.0,y: 0.0,width: image!.size.width,height: image!.size.height), image!.cgImage)

根据CoreGraphics.apinotes的说法,CGContextDrawImage被转换为CGContext.draw

代码语言:javascript
复制
Name: CGContextDrawImage
  # replaced by draw(_ image: CGImage, in rect: CGRect, byTiling: Bool = false)
  SwiftName: CGContext.__draw(self:in:image:)
  SwiftPrivate: true

当我尝试这样做的时候:

代码语言:javascript
复制
 CGContext.draw(context as! CGImage, in: CGRect(x:0.0, y:0.0, width: image!.size.width, height: image!.size.height), byTiling: false) 

似乎有一些简单的语法干扰了编译器,但我看不到(实际上我收到了一个典型的歧义错误):

有人能帮我写一下这个新的swift 3语法代码吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-15 22:53:09

您需要像调用CGContext的实例方法一样调用它

代码语言:javascript
复制
context.draw(image!.cgImage!, in: CGRect(x: 0.0,y: 0.0,width: image!.size.width,height: image!.size.height))

检查the latest reference of CGContext

票数 46
EN

Stack Overflow用户

发布于 2018-07-03 19:30:25

我已经为这个问题找到了另一个非常好的解决方案,我目前正在使用。您只需要在使用UIImagePickerController捕获图像后,将图像作为arugument传递给此方法。它适用于所有版本的iOS,也适用于相机的纵向和横向。它使用UIImageOrientaiton检查图像的EXIF属性,并根据方向的值对图像进行变换和缩放,以便您将获得与相机视图方向相同的返回图像。

在这里,我保持了3000的最大分辨率,这样当你使用视网膜设备时,图像质量就不会受到影响,但你可以根据需要改变它的分辨率。

代码语言:javascript
复制
func scaleAndRotateImage(image: UIImage, MaxResolution iIntMaxResolution: Int) -> UIImage {
        let kMaxResolution = iIntMaxResolution
        let imgRef = image.cgImage!
        let width: CGFloat = CGFloat(imgRef.width)
        let height: CGFloat = CGFloat(imgRef.height)
        var transform = CGAffineTransform.identity
        var bounds = CGRect.init(x: 0, y: 0, width: width, height: height)

        if Int(width) > kMaxResolution || Int(height) > kMaxResolution {
            let ratio: CGFloat = width / height
            if ratio > 1 {
                bounds.size.width = CGFloat(kMaxResolution)
                bounds.size.height = bounds.size.width / ratio
            }
            else {
                bounds.size.height = CGFloat(kMaxResolution)
                bounds.size.width = bounds.size.height * ratio
            }
        }
        let scaleRatio: CGFloat = bounds.size.width / width
        let imageSize = CGSize.init(width: CGFloat(imgRef.width), height: CGFloat(imgRef.height))

        var boundHeight: CGFloat
        let orient = image.imageOrientation
        // The output below is limited by 1 KB.
        // Please Sign Up (Free!) to remove this limitation.

        switch orient {
        case .up:
            //EXIF = 1
            transform = CGAffineTransform.identity
        case .upMirrored:
            //EXIF = 2
            transform = CGAffineTransform.init(translationX: imageSize.width, y: 0.0)
            transform = transform.scaledBy(x: -1.0, y: 1.0)

        case .down:
            //EXIF = 3
            transform = CGAffineTransform.init(translationX: imageSize.width, y: imageSize.height)
            transform = transform.rotated(by: CGFloat(Double.pi / 2))

        case .downMirrored:
            //EXIF = 4
            transform = CGAffineTransform.init(translationX: 0.0, y: imageSize.height)
            transform = transform.scaledBy(x: 1.0, y: -1.0)
        case .leftMirrored:
            //EXIF = 5
            boundHeight = bounds.size.height
            bounds.size.height = bounds.size.width
            bounds.size.width = boundHeight
            transform = CGAffineTransform.init(translationX: imageSize.height, y: imageSize.width)

            transform = transform.scaledBy(x: -1.0, y: 1.0)
            transform = transform.rotated(by: CGFloat(Double.pi / 2) / 2.0)
            break

        default: print("Error in processing image")
        }

        UIGraphicsBeginImageContext(bounds.size)
        let context = UIGraphicsGetCurrentContext()
        if orient == .right || orient == .left {
            context?.scaleBy(x: -scaleRatio, y: scaleRatio)
            context?.translateBy(x: -height, y: 0)
        }
        else {
            context?.scaleBy(x: scaleRatio, y: -scaleRatio)
            context?.translateBy(x: 0, y: -height)
        }
        context?.concatenate(transform)
        context?.draw(imgRef, in: CGRect.init(x: 0, y: 0, width: width, height: height))
        let imageCopy = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return imageCopy!
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39513979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档