2016-07-25 6 views
11

私は(私はXcodeの8ベータ3を使用しています)スウィフト3.0でこのObjective-Cのコードを実装する必要があります。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) 

コードの2行目が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) 
関連する問題