回答編集履歴
2
修正
test
CHANGED
@@ -13,3 +13,73 @@
|
|
13
13
|
imageView.clipsToBounds = true
|
14
14
|
|
15
15
|
```
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
回答追記
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```swift
|
26
|
+
|
27
|
+
import UIKit
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
class ViewController: UIViewController {
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
@IBOutlet weak var imageView: UIImageView!
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
override func viewDidLoad() {
|
40
|
+
|
41
|
+
super.viewDidLoad()
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
imageView.image = UIImage(named: "apple")?.resize(imageView.frame.width)
|
46
|
+
|
47
|
+
imageView.contentMode = .Top
|
48
|
+
|
49
|
+
imageView.clipsToBounds = true
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
print(imageView.image?.size)
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
extension UIImage {
|
62
|
+
|
63
|
+
func resize(imageViewWidth: CGFloat) -> UIImage {
|
64
|
+
|
65
|
+
let ratio = imageViewWidth / self.size.width
|
66
|
+
|
67
|
+
let resizedSize = CGSize(width: (self.size.width * ratio), height: (self.size.height * ratio))
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
UIGraphicsBeginImageContextWithOptions(resizedSize, false, 0.0)
|
72
|
+
|
73
|
+
drawInRect(CGRect(x: 0, y: 0, width: resizedSize.width, height: resizedSize.height))
|
74
|
+
|
75
|
+
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
|
76
|
+
|
77
|
+
UIGraphicsEndImageContext()
|
78
|
+
|
79
|
+
return resizedImage
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
```
|
1
修正
test
CHANGED
@@ -4,4 +4,12 @@
|
|
4
4
|
|
5
5
|
imageView.layer.masksToBounds = true
|
6
6
|
|
7
|
+
|
8
|
+
|
9
|
+
or
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
imageView.clipsToBounds = true
|
14
|
+
|
7
15
|
```
|