2016-07-25 62 views
11

我需要實現雨燕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()最新文檔中任何事情。

回答

21

當我寫這篇文章的代碼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最新的文檔,

CGImageCreateWithImageInRect

點擊鏈接雨燕,它表明:

func cropping(to rect: CGRect) -> CGImage? 
1
var imageRef = self.image.cropping(to: cropRect)