【问题标题】:Swift 3.0: How to call CGImageCreateWithImageInRect()?Swift 3.0:如何调用 CGImageCreateWithImageInRect()?
【发布时间】:2016-07-25 12:50:54
【问题描述】:

我需要在 Swift 3.0 中实现这个 Objective-C 代码(我使用的是 Xcode 8 Beta 3):

// Note: this code comes from an Obj-C category on UIImage
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, cropRect);
UIImage *image = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];

我在CGImageCreateWithImageInRect() 的最新文档中找不到任何内容。

【问题讨论】:

    标签: swift cgimage swift3 xcode8-beta3


    【解决方案1】:

    当我在 Xcode8 的 Swift 编辑器中编写这段代码时:

    let imageRef = CGImageCreateWithImageInRect(self.CGImage!, cropRect)
    

    Swift 向我显示了一个错误:

    error: 'CGImageCreateWithImageInRect' has been replaced by instance method 'CGImage.cropping(to:)'
    

    所以,我将这一行改为:

    let imageRef = self.cgImage!.cropping(to: cropRect)
    

    而且您的代码的第二行似乎可以直接转换为 Swift:

    let image = UIImage(cgImage: imageRef!, scale: self.scale, orientation: self.imageOrientation)
    

    还有CGImageCreateWithImageInRect的最新文档,

    CGImageCreateWithImageInRect

    点击 Swift 链接,它显示:

    func cropping(to rect: CGRect) -> CGImage?
    

    【讨论】:

      【解决方案2】:
      var imageRef = self.image.cropping(to: cropRect)
      

      【讨论】:

        猜你喜欢
        • 2017-07-10
        • 1970-01-01
        • 2012-02-10
        • 2018-02-12
        • 1970-01-01
        • 2016-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多