質問編集履歴

3

拡大・縮小のファイル追加

2017/05/29 14:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ・表示されている画像を走査して、加工をしたい。
6
6
 
7
- その前段として、画像走査してdataを作り、そのdataから画像を再生成したい。
7
+ ・画像走査
8
8
 
9
9
 
10
10
 
@@ -46,12 +46,372 @@
46
46
 
47
47
  ```ここに言語を入力
48
48
 
49
+ ----画像拡大----
50
+
51
+
52
+
53
+ import UIKit
54
+
55
+ import Photos
56
+
57
+
58
+
59
+ class FAScrollView: UIScrollView{
60
+
61
+
62
+
63
+ // MARK: Class properties
64
+
65
+
66
+
67
+ var imageView:UIImageView = UIImageView()
68
+
69
+ var imageToDisplay:UIImage? = nil{
70
+
71
+ didSet{
72
+
73
+ zoomScale = 1.0
74
+
75
+ minimumZoomScale = 1.0
76
+
77
+ imageView.image = imageToDisplay
78
+
79
+ imageView.frame.size = sizeForImageToDisplay()
80
+
81
+ imageView.center = center
82
+
83
+ contentSize = imageView.frame.size
84
+
85
+ contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
86
+
87
+ updateLayout()
88
+
89
+ }
90
+
91
+ }
92
+
93
+ var gridView:UIView = Bundle.main.loadNibNamed("FAGridView", owner: nil, options: nil)?.first as! UIView
94
+
95
+
96
+
97
+
98
+
99
+ // MARK : Class Functions
100
+
101
+
102
+
103
+ override func awakeFromNib() {
104
+
105
+ super.awakeFromNib()
106
+
107
+ viewConfigurations()
108
+
109
+ }
110
+
111
+
112
+
113
+ func updateLayout() {
114
+
115
+ imageView.center = center;
116
+
117
+ var frame:CGRect = imageView.frame;
118
+
119
+ if (frame.origin.x < 0) { frame.origin.x = 0 }
120
+
121
+ if (frame.origin.y < 0) { frame.origin.y = 0 }
122
+
123
+ imageView.frame = frame
124
+
125
+ }
126
+
127
+
128
+
129
+ func zoom() {
130
+
131
+ if (zoomScale <= 1.0) { setZoomScale(zoomScaleWithNoWhiteSpaces(), animated: true) }
132
+
133
+ else{ setZoomScale(minimumZoomScale, animated: true) }
134
+
135
+ updateLayout()
136
+
137
+ }
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+ ---画像拡大→画面遷移----
146
+
147
+
148
+
149
+ import UIKit
150
+
151
+ import Photos
152
+
153
+
154
+
155
+ class FAImageCropperVC: UIViewController {
156
+
157
+
158
+
159
+ // MARK: IBOutlets
160
+
161
+
162
+
163
+ @IBOutlet weak var scrollContainerView: UIView!
164
+
165
+ @IBOutlet weak var scrollView: FAScrollView!
166
+
167
+ @IBOutlet weak var collectionView: UICollectionView!
168
+
169
+ @IBOutlet weak var btnZoom: UIButton!
170
+
171
+ @IBOutlet weak var btnCrop: UIButton!
172
+
173
+ @IBAction func zoom(_ sender: Any) {
174
+
175
+ scrollView.zoom()
176
+
177
+ }
178
+
179
+ @IBAction func crop(_ sender: Any) {
180
+
181
+ croppedImage = captureVisibleRect()
182
+
183
+ performSegue(withIdentifier: "FADetailViewSegue", sender: nil)
184
+
185
+ }
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+ // MARK: Public Properties
194
+
195
+
196
+
197
+ var photos:[PHAsset]!
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+ // MARK: Private Properties
206
+
207
+
208
+
209
+ private let imageLoader = FAImageLoader()
210
+
211
+ private var croppedImage: UIImage? = nil
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+ // MARK: LifeCycle
220
+
221
+
222
+
223
+ override func viewDidLoad() {
224
+
225
+ super.viewDidLoad()
226
+
227
+ viewConfigurations() // zoom,cropボタン
228
+
229
+ checkForPhotosPermission()
230
+
231
+ }
232
+
233
+
234
+
235
+ override func didReceiveMemoryWarning() {
236
+
237
+ super.didReceiveMemoryWarning()
238
+
239
+ // Dispose of any resources that can be recreated.
240
+
241
+ }
242
+
243
+
244
+
245
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
246
+
247
+
248
+
249
+ if segue.identifier == "FADetailViewSegue" {
250
+
251
+
252
+
253
+ let detailVC = segue.destination as? FADetailVC
254
+
255
+ detailVC?.croppedImage = croppedImage
256
+
257
+ }
258
+
259
+ }
260
+
261
+
262
+
263
+ // MARK: Private Functions
264
+
265
+
266
+
267
+ private func checkForPhotosPermission(){
268
+
269
+ //※省略
270
+
271
+ }
272
+
273
+ private func viewConfigurations() {
274
+
275
+ btnCrop.layer.cornerRadius = btnCrop.frame.size.width/2
276
+
277
+ btnZoom.layer.cornerRadius = btnZoom.frame.size.width/2
278
+
279
+ }
280
+
281
+
282
+
283
+ private func configureImageCropper(assets:[PHAsset]){
284
+
285
+
286
+
287
+ if assets.count != 0{
288
+
289
+ photos = assets
290
+
291
+ collectionView.delegate = self
292
+
293
+ collectionView.dataSource = self
294
+
295
+ collectionView.reloadData()
296
+
297
+ selectDefaultImage()
298
+
299
+ }
300
+
301
+ }
302
+
303
+
304
+
305
+ private func selectDefaultImage(){
306
+
307
+ collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .top)
308
+
309
+ selectImageFromAssetAtIndex(index: 0)
310
+
311
+ }
312
+
313
+
314
+
315
+
316
+
317
+ private func captureVisibleRect() -> UIImage{
318
+
319
+
320
+
321
+ var croprect = CGRect.zero
322
+
323
+ let xOffset = (scrollView.imageToDisplay?.size.width)! / scrollView.contentSize.width;
324
+
325
+ let yOffset = (scrollView.imageToDisplay?.size.height)! / scrollView.contentSize.height;
326
+
327
+
328
+
329
+ croprect.origin.x = scrollView.contentOffset.x * xOffset;
330
+
331
+ croprect.origin.y = scrollView.contentOffset.y * yOffset;
332
+
333
+
334
+
335
+ let normalizedWidth = (scrollView?.frame.width)! / (scrollView?.contentSize.width)!
336
+
337
+ let normalizedHeight = (scrollView?.frame.height)! / (scrollView?.contentSize.height)!
338
+
339
+
340
+
341
+ croprect.size.width = scrollView.imageToDisplay!.size.width * normalizedWidth
342
+
343
+ croprect.size.height = scrollView.imageToDisplay!.size.height * normalizedHeight
344
+
345
+
346
+
347
+ let cr: CGImage? = scrollView.imageView.image?.cgImage?.cropping(to: croprect)
348
+
349
+ let cropped = UIImage(cgImage: cr!)
350
+
351
+
352
+
353
+ return cropped
354
+
355
+
356
+
357
+ }
358
+
359
+ private func isSquareImage() -> Bool{
360
+
361
+ let image = scrollView.imageToDisplay
362
+
363
+ if image?.size.width == image?.size.height { return true }
364
+
365
+ else { return false }
366
+
367
+ }
368
+
369
+
370
+
371
+
372
+
373
+ // MARK: Public Functions
374
+
375
+
376
+
377
+ func selectImageFromAssetAtIndex(index:NSInteger){
378
+
379
+
380
+
381
+ FAImageLoader.imageFrom(asset: photos[index], size: PHImageManagerMaximumSize) { (image) in
382
+
383
+ DispatchQueue.main.async {
384
+
385
+ self.displayImageInScrollView(image: image)
386
+
387
+ }
388
+
389
+ }
390
+
391
+ }
392
+
393
+
394
+
395
+ func displayImageInScrollView(image:UIImage){
396
+
397
+ self.scrollView.imageToDisplay = image
398
+
399
+ if isSquareImage() { btnZoom.isHidden = true }
400
+
401
+ else { btnZoom.isHidden = false }
402
+
403
+ }
404
+
405
+ }
406
+
407
+
408
+
409
+
410
+
49
411
  ----呼び出し元------
50
412
 
51
413
  private func viewConfigurations(){
52
414
 
53
- print("viewconfig呼ばれる")
54
-
55
415
  imageView.image = croppedImage
56
416
 
57
417
  imageView.contentMode = UIViewContentMode.scaleAspectFit
@@ -120,7 +480,7 @@
120
480
 
121
481
  let imageData = image.cgImage!.dataProvider!.data //cgimageのデータ
122
482
 
123
- let datalenght = CFDataGetLength(imageData) //表示されている画像の長さとも違う。。
483
+ let datalenght = CFDataGetLength(imageData)
124
484
 
125
485
  var rawData = [UInt8](repeating: 0, count: datalenght)
126
486
 
@@ -170,12 +530,6 @@
170
530
 
171
531
 
172
532
 
173
-
174
-
175
-
176
-
177
-
178
-
179
533
  ```
180
534
 
181
535
 

2

ソースと具体的な事象を更新

2017/05/29 14:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,9 +12,9 @@
12
12
 
13
13
  PC上のシミュレーターでは処理が上手くいくのだが、実機を用いたテストで上手くいかない。
14
14
 
15
- エラー吐いていないが、処理が正常終了していない
15
+ 具体的には、ボタンクリックで該当の処理が走り、画像表示されるはずだが、画像が更新されない
16
16
 
17
- 下記のボタンクリックあと、ボタンが押した状態から戻らない(具体的には、ボタンの文字が白っぽくなって戻らない他の処理を動かそうとしてもう)
17
+ ボタンクリックされた状態から戻らない。
18
18
 
19
19
 
20
20
 
@@ -86,7 +86,7 @@
86
86
 
87
87
 
88
88
 
89
- ----呼び出し先------
89
+ ----呼び出し先(TransparentImage)------
90
90
 
91
91
  import UIKit
92
92
 
@@ -102,69 +102,39 @@
102
102
 
103
103
 
104
104
 
105
+
106
+
105
107
  let editedimage:UIImage
106
108
 
109
+ // let width :Int = Int((image.size.width))
110
+
107
111
  let width :Int = Int((image.cgImage!.width))
112
+
113
+ // let height :Int = Int((image.size.height))
108
114
 
109
115
  let height :Int = Int((image.cgImage!.height))
110
116
 
111
117
 
112
118
 
113
- // /* ver1
119
+
114
-
115
- let scale :Int = Int(UIScreen.main.scale) //
116
120
 
117
121
  let imageData = image.cgImage!.dataProvider!.data //cgimageのデータ
118
122
 
119
- let datalenght = CFDataGetLength(imageData)
123
+ let datalenght = CFDataGetLength(imageData) //表示されている画像の長さとも違う。。
120
124
 
121
125
  var rawData = [UInt8](repeating: 0, count: datalenght)
122
126
 
123
- CFDataGetBytes(imageData,CFRange(location: 0, length: datalenght),&rawData)
127
+ CFDataGetBytes(imageData,CFRange(location: 0, length: datalenght),&rawData) //画像データをrawDataへ転送
124
-
125
- print("datalenght:\(datalenght)") //画像のdatalengthの長さ
126
-
127
-
128
-
129
-
130
-
131
- var address : Int
132
-
133
-
134
-
135
-
136
-
137
- //画像の走査処理
138
-
139
- for x in 0..<width{
140
-
141
- for y in 0..<height{
142
-
143
- address = ((width * y) + x) * 4
144
128
 
145
129
 
146
130
 
147
131
 
148
132
 
149
- rawData[address+1] = 0
133
+ let provider = CGDataProvider(dataInfo: nil,data: rawData, size: datalenght,releaseData: releaseData)
150
134
 
151
- }
135
+ let colorSpaceRef = image.cgImage!.colorSpace
152
136
 
153
- }
154
-
155
- let provider = CGDataProvider(dataInfo: nil,data: rawData, size: datalenght,releaseData: releaseData)
156
-
157
-
158
-
159
- //CGImage(ビットマップ画像)を作成
160
-
161
- let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
137
+ let bitmapInfo = image.cgImage!.bitmapInfo
162
-
163
- let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue) /
164
-
165
-
166
-
167
- //実データより取得
168
138
 
169
139
  let bitsPerComponent:Int = image.cgImage!.bitsPerComponent
170
140
 
@@ -172,23 +142,23 @@
172
142
 
173
143
  let bytesPerRow = image.cgImage!.bytesPerRow
174
144
 
175
- let cgImage = CGImage(width: width, height: height, bitsPerComponent: bitsPerComponent, bitsPerPixel: bitsPerPixel, bytesPerRow:bytesPerRow, space: colorSpaceRef, bitmapInfo: bitmapInfo, provider: provider!, decode: nil, shouldInterpolate: false, intent: .defaultIntent)
145
+ let cgImage = CGImage(width: width, height: height, bitsPerComponent: bitsPerComponent, bitsPerPixel: bitsPerPixel, bytesPerRow:bytesPerRow, space: colorSpaceRef!, bitmapInfo: bitmapInfo, provider: provider!, decode: nil, shouldInterpolate: false, intent: .defaultIntent)
176
146
 
177
-
178
-
179
- editedimage = UIImage(cgImage:cgImage!, scale:CGFloat(scale), orientation:UIImageOrientation.up)
147
+ editedimage = UIImage(cgImage:cgImage!, scale:image.scale, orientation:image.imageOrientation)
180
148
 
181
149
 
182
150
 
183
151
  return editedimage
184
-
185
-
186
152
 
187
153
  }
188
154
 
189
155
 
190
156
 
191
157
  let releaseData: CGDataProviderReleaseDataCallback = { (info: UnsafeMutableRawPointer?, data: UnsafeRawPointer, size: Int) -> () in
158
+
159
+ // https://developer.apple.com/reference/coregraphics/cgdataproviderreleasedatacallback
160
+
161
+ // N.B. 'CGDataProviderRelease' is unavailable: Core Foundation objects are automatically memory managed
192
162
 
193
163
  print("callback")
194
164
 
@@ -197,8 +167,6 @@
197
167
  }
198
168
 
199
169
  }
200
-
201
-
202
170
 
203
171
 
204
172
 

1

ソースを修正

2017/05/27 04:47

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
 
44
44
 
45
- ###該当のソースコード
45
+ ###該当のソースコード ※修正しました
46
46
 
47
47
  ```ここに言語を入力
48
48
 
@@ -54,7 +54,7 @@
54
54
 
55
55
  imageView.image = croppedImage
56
56
 
57
- imageView.contentMode = UIViewContentMode.scaleAspectFill
57
+ imageView.contentMode = UIViewContentMode.scaleAspectFit
58
58
 
59
59
  }
60
60
 
@@ -104,18 +104,26 @@
104
104
 
105
105
  let editedimage:UIImage
106
106
 
107
- let width :Int = Int((image.size.width))
107
+ let width :Int = Int((image.cgImage!.width))
108
-
108
+
109
- let height :Int = Int((image.size.height))
109
+ let height :Int = Int((image.cgImage!.height))
110
+
111
+
112
+
110
-
113
+ // /* ver1
114
+
115
+ let scale :Int = Int(UIScreen.main.scale) //
116
+
111
- let imageData = image.cgImage!.dataProvider!.data
117
+ let imageData = image.cgImage!.dataProvider!.data //cgimageのデータ
112
-
118
+
113
- let datalenght = CFDataGetLength(imageData)
119
+ let datalenght = CFDataGetLength(imageData)
114
120
 
115
121
  var rawData = [UInt8](repeating: 0, count: datalenght)
116
122
 
117
123
  CFDataGetBytes(imageData,CFRange(location: 0, length: datalenght),&rawData)
118
124
 
125
+ print("datalenght:\(datalenght)") //画像のdatalengthの長さ
126
+
119
127
 
120
128
 
121
129
 
@@ -128,52 +136,54 @@
128
136
 
129
137
  //画像の走査処理
130
138
 
131
- for y in 0...height-2{
132
-
133
- for x in 1...width{
139
+ for x in 0..<width{
140
+
141
+ for y in 0..<height{
134
142
 
135
143
  address = ((width * y) + x) * 4
136
144
 
137
145
 
138
146
 
147
+
148
+
139
149
  rawData[address+1] = 0
140
150
 
141
- rawData[address] = 0
142
-
143
- rawData[address+2] = 0
144
-
145
- rawData[address+3] = 0
146
-
147
151
  }
148
152
 
149
153
  }
150
154
 
151
- // */
152
-
153
- //CGDataProvider(データの場所や種類の情報)を作成
154
-
155
155
  let provider = CGDataProvider(dataInfo: nil,data: rawData, size: datalenght,releaseData: releaseData)
156
156
 
157
157
 
158
158
 
159
159
  //CGImage(ビットマップ画像)を作成
160
160
 
161
- let bitsPerComponent:Int = 8 //画素は8bit
162
-
163
- let bitsPerPixel:Int = bitsPerComponent * 4 //pxあたりのビット数:4色より
164
-
165
- let bytesPerRow:Int = Int(4) * width //1列あたりのバイト数
166
-
167
161
  let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
168
162
 
169
- let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
163
+ let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue) /
164
+
165
+
166
+
167
+ //実データより取得
168
+
169
+ let bitsPerComponent:Int = image.cgImage!.bitsPerComponent
170
+
171
+ let bitsPerPixel = image.cgImage!.bitsPerPixel
172
+
173
+ let bytesPerRow = image.cgImage!.bytesPerRow
170
174
 
171
175
  let cgImage = CGImage(width: width, height: height, bitsPerComponent: bitsPerComponent, bitsPerPixel: bitsPerPixel, bytesPerRow:bytesPerRow, space: colorSpaceRef, bitmapInfo: bitmapInfo, provider: provider!, decode: nil, shouldInterpolate: false, intent: .defaultIntent)
172
176
 
177
+
178
+
173
- editedimage = UIImage(cgImage: cgImage!)
179
+ editedimage = UIImage(cgImage:cgImage!, scale:CGFloat(scale), orientation:UIImageOrientation.up)
180
+
181
+
174
182
 
175
183
  return editedimage
176
184
 
185
+
186
+
177
187
  }
178
188
 
179
189