Swift 3.0:如何调用CGImageCreateWithImageInRect()?

16

我需要在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()的内容。

2个回答

39

当我在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?

1
var imageRef = self.image.cropping(to: cropRect)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接