Swift 3.0:如何调用CGImageCreateWithImageInRect()?

Swift 3.0: How to call CGImageCreateWithImageInRect()?

我需要在 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() 的最新文档中找不到任何内容。

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

当我在 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 link,显示:

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