我需要在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()
的最新文档中找不到任何内容。
答案 0 :(得分:30)
当我在Xcode8&#swft编辑器中编写此代码时:
let imageRef = CGImageCreateWithImageInRect(self.CGImage!, cropRect)
斯威夫特向我展示了一个错误:
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
,
单击Swift链接,显示:
func cropping(to rect: CGRect) -> CGImage?
答案 1 :(得分:1)
var imageRef = self.image.cropping(to: cropRect)