95x95の画像の左上の辺りをくり抜いてみます。
swift
1if let image = UIImage(named: "marimo.jpg") {
2 print("size=", image.size)
3 //=> size= (95.0, 95.0)
4
5 //コンテキスト作成/取得
6 UIGraphicsBeginImageContext(image.size)
7 let context = UIGraphicsGetCurrentContext()
8
9 //コンテキストにimageをそのまま描画
10 image.drawInRect(CGRect(origin: CGPointZero, size: image.size))
11
12 //くり抜く矩形
13 let rect = CGRect(x: 10, y: 20, width: 30, height: 40)
14 print("rect=", rect)
15 //=> rect= (10.0, 20.0, 30.0, 40.0)
16
17 //くり抜き
18 CGContextClearRect(context, rect)
19
20 //くり抜いた画像をUIImageViewにセット
21 if let cgImage = CGBitmapContextCreateImage(context) {
22 self.imageView.image = UIImage(CGImage: cgImage)
23 }
24
25 //コンテキスト削除
26 UIGraphicsEndImageContext()
27}
オレンジ色はUIImageViewのbackgroundColorです。
Swift2なので、Swift3の場合は適当に修正して下さい。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/01 02:43