Swift 3.0:如何调用CGImageCreateWithImageInRect()?
问题描述:
我需要实现雨燕3.0本Objective-C代码(我使用的Xcode 8 Beta 3中):Swift 3.0:如何调用CGImageCreateWithImageInRect()?
// 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()
最新文档中任何事情。
答
当我写这篇文章的代码Xcode8的斯威夫特编辑:
let imageRef = CGImageCreateWithImageInRect(self.CGImage!, cropRect)
斯威夫特已经表明了我一个错误:
error: 'CGImageCreateWithImageInRect' has been replaced by instance method 'CGImage.cropping(to:)'
于是,我改变了线路:
let imageRef = self.cgImage!.cropping(to: cropRect)
而你的代码的第二行似乎是di rectly转换为斯威夫特:
let image = UIImage(cgImage: imageRef!, scale: self.scale, orientation: self.imageOrientation)
和CGImageCreateWithImageInRect
最新的文档,
点击链接雨燕,它表明:
func cropping(to rect: CGRect) -> CGImage?
答
var imageRef = self.image.cropping(to: cropRect)