回答編集履歴
1
CGContextClearRectを使ってみる。
test
CHANGED
@@ -1,5 +1,73 @@
|
|
1
|
-
|
1
|
+
95x95の画像の左上の辺りをくり抜いてみます。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
```swift
|
6
|
+
|
7
|
+
if let image = UIImage(named: "marimo.jpg") {
|
8
|
+
|
9
|
+
print("size=", image.size)
|
10
|
+
|
11
|
+
//=> size= (95.0, 95.0)
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
//コンテキスト作成/取得
|
16
|
+
|
17
|
+
UIGraphicsBeginImageContext(image.size)
|
18
|
+
|
19
|
+
let context = UIGraphicsGetCurrentContext()
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
//コンテキストにimageをそのまま描画
|
24
|
+
|
25
|
+
image.drawInRect(CGRect(origin: CGPointZero, size: image.size))
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
//くり抜く矩形
|
30
|
+
|
31
|
+
let rect = CGRect(x: 10, y: 20, width: 30, height: 40)
|
32
|
+
|
33
|
+
print("rect=", rect)
|
34
|
+
|
35
|
+
//=> rect= (10.0, 20.0, 30.0, 40.0)
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
//くり抜き
|
40
|
+
|
5
|
-
CGRect
|
41
|
+
CGContextClearRect(context, rect)
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
//くり抜いた画像をUIImageViewにセット
|
46
|
+
|
47
|
+
if let cgImage = CGBitmapContextCreateImage(context) {
|
48
|
+
|
49
|
+
self.imageView.image = UIImage(CGImage: cgImage)
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
//コンテキスト削除
|
56
|
+
|
57
|
+
UIGraphicsEndImageContext()
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
オレンジ色はUIImageViewのbackgroundColorです。
|
66
|
+
|
67
|
+
![marimo](b4362b3486f4731ea70e13b24249226e.png)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
Swift2なので、Swift3の場合は適当に修正して下さい。
|
72
|
+
|
73
|
+
|