LoginSignup
12
13

More than 5 years have passed since last update.

[Swift3] UIImageを切り抜く(とおまけ)

Last updated at Posted at 2016-09-22

こちらのStackOverflowで既出ですが、Swift3でUIImageを特定の大きさに切り抜く方法が変わったのでメモしておきます。

Swift2まで

Swift2までは以下のようなコードで切り抜くことができました。

let imageRef = CGImageCreateWithImageInRect(image.CGImage, CGRect)
let image = UIImage(CGImage: imageRef)

Swift3から

Swift3では次のように変わります。

let imgRef = image.cgImage?.cropping(to: CGRect)
let image = UIImage(cgImage: imgRef!, scale: image.scale, orientation: image.imageOrientation)

おまけ

この変更とともに、CGほにゃらら系の宣言の仕方が変わっています。いままではCGRectMakeCGSizeMakeなどでも宣言できていましたがこれが廃止されました。またイニシャライザについて少し種類が増えているようです(?)

最後に

Swift3やXcode8について他にも記事を書いているので参考にしてください。

12
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13