質問編集履歴
1
ある程度自分で修正を加えた
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,26 @@
|
|
1
|
+
以下の参考サイトを参考にSwift4に変えながらコードを書いていたところ、
|
2
|
+
|
3
|
+
```Swift
|
4
|
+
|
5
|
+
imageCropVC.delegate = self as? RSKImageCropViewControllerDelegate
|
6
|
+
|
7
|
+
imageCropVC.dataSource = self as! RSKImageCropViewControllerDataSource
|
8
|
+
|
9
|
+
```
|
10
|
+
|
1
|
-
|
11
|
+
の部分でThread 1: signal SIGABRTとエラーが出てしまいます。実行するまでは何もエラーが出ないのですが写真を選択するとアプリが落ちてエラーが出ます。
|
12
|
+
|
13
|
+
どうしたらよいでしょうか?
|
14
|
+
|
15
|
+
|
2
16
|
|
3
17
|
|
4
18
|
|
5
19
|
[参考サイト](http://kimagureneet.hatenablog.com/entry/2015/08/19/010120)
|
6
20
|
|
21
|
+
|
22
|
+
|
7
|
-
|
23
|
+
コード全体
|
8
|
-
|
9
|
-
|
10
24
|
|
11
25
|
```Swift
|
12
26
|
|
@@ -16,148 +30,188 @@
|
|
16
30
|
|
17
31
|
|
18
32
|
|
19
|
-
class
|
33
|
+
class SettingViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
34
|
+
|
35
|
+
|
36
|
+
|
20
|
-
|
37
|
+
var selectedimage: UIImage?
|
38
|
+
|
39
|
+
let userDefaults = UserDefaults.standard
|
40
|
+
|
21
|
-
override func viewDidLoad() {
|
41
|
+
override func viewDidLoad() {
|
22
42
|
|
23
43
|
super.viewDidLoad()
|
24
44
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
i
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
45
|
+
// Do any additional setup after loading the view.
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
override func didReceiveMemoryWarning() {
|
52
|
+
|
53
|
+
super.didReceiveMemoryWarning()
|
54
|
+
|
55
|
+
// Dispose of any resources that can be recreated.
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
@IBAction func picPicture(_ sender: Any) {
|
62
|
+
|
63
|
+
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
|
64
|
+
|
65
|
+
let pickerView = UIImagePickerController()
|
66
|
+
|
67
|
+
pickerView.sourceType = .photoLibrary
|
68
|
+
|
69
|
+
pickerView.delegate = self
|
70
|
+
|
71
|
+
self.present(pickerView, animated: true)
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
|
80
|
+
|
81
|
+
selectedimage = info[UIImagePickerControllerOriginalImage] as! UIImage
|
82
|
+
|
83
|
+
self.dismiss(animated: true)
|
84
|
+
|
85
|
+
if let image = selectedimage {
|
86
|
+
|
87
|
+
var imageCropVC: RSKImageCropViewController = RSKImageCropViewController(image: image, cropMode: RSKImageCropMode.custom)
|
88
|
+
|
89
|
+
imageCropVC.delegate = self as? RSKImageCropViewControllerDelegate
|
90
|
+
|
91
|
+
imageCropVC.dataSource = self as! RSKImageCropViewControllerDataSource
|
92
|
+
|
93
|
+
self.navigationController?.pushViewController(imageCropVC, animated: true)
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
// キャンセルがおされたらトリミング画面を閉じます
|
102
|
+
|
103
|
+
func imageCropViewControllerDidCancelCrop(controller: RSKImageCropViewController!) {
|
104
|
+
|
105
|
+
self.navigationController?.popViewController(animated: true)
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
func imageCropViewController(controller: RSKImageCropViewController!, willCropImage originalImage: UIImage!) {
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
// トリミング済みの画像がかえされます
|
118
|
+
|
119
|
+
func imageCropViewController(controller: RSKImageCropViewController!, didCropImage croppedImage: UIImage!, usingCropRect cropRect: CGRect) {
|
120
|
+
|
121
|
+
let imageData = UIImageJPEGRepresentation(croppedImage, 1);
|
122
|
+
|
123
|
+
userDefaults.set(imageData, forKey: "image")
|
124
|
+
|
125
|
+
userDefaults.synchronize()
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
func imageCropViewControllerCustomMaskRect(controller: RSKImageCropViewController!) -> CGRect {
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
var maskSize: CGSize
|
136
|
+
|
137
|
+
var width, height: CGFloat!
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
width = self.view.frame.width
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
// 縦横比 = 1 : 2でトリミングしたい場合
|
146
|
+
|
147
|
+
height = self.view.frame.width / 2
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
maskSize = CGSize(width: self.view.frame.width, height: height)
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
var viewWidth: CGFloat = controller.view.frame.width
|
156
|
+
|
157
|
+
var viewHeight: CGFloat = controller.view.frame.height
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
var maskRect: CGRect = CGRect(x: (viewWidth - maskSize.width) * 0.5, y: (viewHeight - maskSize.height) * 0.5, width: maskSize.width, height: maskSize.height)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
return maskRect
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
// トリミングしたい領域を描画(今回は四角な領域です・・・)
|
172
|
+
|
173
|
+
func imageCropViewControllerCustomMaskPath(controller: RSKImageCropViewController!) -> UIBezierPath! {
|
174
|
+
|
175
|
+
var rect: CGRect = controller.maskRect
|
176
|
+
|
177
|
+
var point1: CGPoint = CGPoint(x: rect.minX, y:rect.maxY)
|
178
|
+
|
179
|
+
var point2: CGPoint = CGPoint(x: rect.maxX, y:rect.maxY)
|
180
|
+
|
181
|
+
var point3: CGPoint = CGPoint(x: rect.maxX, y:rect.minY)
|
182
|
+
|
183
|
+
var point4: CGPoint = CGPoint(x: rect.minX, y:rect.minY)
|
184
|
+
|
185
|
+
var square: UIBezierPath = UIBezierPath()
|
186
|
+
|
187
|
+
square.move(to: point1)
|
188
|
+
|
189
|
+
square.addLine(to: point2)
|
190
|
+
|
191
|
+
square.addLine(to: point3)
|
192
|
+
|
193
|
+
square.addLine(to: point4)
|
194
|
+
|
195
|
+
square.close()
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
return square
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
func imageCropViewControllerCustomMovementRect(controller: RSKImageCropViewController!) -> CGRect {
|
206
|
+
|
207
|
+
return controller.maskRect
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
40
212
|
|
41
213
|
}
|
42
214
|
|
43
215
|
|
44
216
|
|
45
|
-
// キャンセルがおされたらトリミング画面を閉じます
|
46
|
-
|
47
|
-
func imageCropViewControllerDidCancelCrop(controller: RSKImageCropViewController!) {
|
48
|
-
|
49
|
-
self.navigationController?.popViewControllerAnimated(true)
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
// トリミング前に呼ばれるようです今回は使っていませんが、ないとコンパイルできないので定義しておきます
|
56
|
-
|
57
|
-
func imageCropViewController(controller: RSKImageCropViewController!, willCropImage originalImage: UIImage!) {
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
// トリミング済みの画像がかえされます
|
64
|
-
|
65
|
-
func imageCropViewController(controller: RSKImageCropViewController!, didCropImage croppedImage: UIImage!, usingCropRect cropRect: CGRect) {
|
66
|
-
|
67
|
-
// 画像をサーバにアップロードするなり・・・
|
68
|
-
|
69
|
-
// 画像を画面にセットしたり・・・
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
func imageCropViewControllerCustomMaskRect(controller: RSKImageCropViewController!) -> CGRect {
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
var maskSize: CGSize
|
78
|
-
|
79
|
-
var width, height: CGFloat!
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
width = self.view.frame.width
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
// 縦横比 = 1 : 2でトリミングしたい場合
|
88
|
-
|
89
|
-
height = self.view.frame.width / 2
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
// 正方形でトリミングしたい場合
|
94
|
-
|
95
|
-
height = self.view.frame.width
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
maskSize = CGSizeMake(self.view.frame.width, height)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
var viewWidth: CGFloat = CGRectGetWidth(controller.view.frame)
|
104
|
-
|
105
|
-
var viewHeight: CGFloat = CGRectGetHeight(controller.view.frame)
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
var maskRect: CGRect = CGRectMake((viewWidth - maskSize.width) * 0.5, (viewHeight - maskSize.height) * 0.5, maskSize.width, maskSize.height)
|
110
|
-
|
111
|
-
return maskRect
|
112
|
-
|
113
|
-
}
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
// トリミングしたい領域を描画(今回は四角な領域です・・・)
|
118
|
-
|
119
|
-
func imageCropViewControllerCustomMaskPath(controller: RSKImageCropViewController!) -> UIBezierPath! {
|
120
|
-
|
121
|
-
var rect: CGRect = controller.maskRect
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
var point1: CGPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect))
|
126
|
-
|
127
|
-
var point2: CGPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect))
|
128
|
-
|
129
|
-
var point3: CGPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))
|
130
|
-
|
131
|
-
var point4: CGPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
var square: UIBezierPath = UIBezierPath()
|
136
|
-
|
137
|
-
square.moveToPoint(point1)
|
138
|
-
|
139
|
-
square.addLineToPoint(point2)
|
140
|
-
|
141
|
-
square.addLineToPoint(point3)
|
142
|
-
|
143
|
-
square.addLineToPoint(point4)
|
144
|
-
|
145
|
-
square.closePath()
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
return square
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
func imageCropViewControllerCustomMovementRect(controller: RSKImageCropViewController!) -> CGRect {
|
156
|
-
|
157
|
-
return controller.maskRect
|
158
|
-
|
159
|
-
}
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
217
|
```
|