質問編集履歴
2
ソース掲載
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,6 +14,26 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
+
**[追記]**
|
18
|
+
|
19
|
+
質問に対して、ご指摘ありがとうございます。
|
20
|
+
|
21
|
+
また、投げやりな質問で大変失礼致しました。気をつけたいと思います。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
やりたいことは指定のアルバム(MyCollection)から画像を取得してCollectionViewに表示したいです。
|
26
|
+
|
27
|
+
色々自分なりに調べ、なんとかアルバムの写真のデータを取得できました。
|
28
|
+
|
29
|
+
しかし、CollectionViewで表示すると最後の1枚しか表示されませんでした。
|
30
|
+
|
31
|
+
(アルバム「MyCollection」には3枚写真があります)
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
配列の入れ方に問題があるのでしょうか。
|
36
|
+
|
17
37
|
|
18
38
|
|
19
39
|
###該当のソースコード
|
@@ -62,14 +82,248 @@
|
|
62
82
|
|
63
83
|
}
|
64
84
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
85
|
```
|
72
86
|
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
[**修正**]
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
class PhotoViewController : UIViewController,UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
var dataArray :[String] = []
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
// var key : String = ""
|
108
|
+
|
109
|
+
// var value2 :String = ""
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
var photosImg:[UIImage] = []
|
114
|
+
|
115
|
+
var cellImage:UIImage?
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
override func viewDidLoad() {
|
120
|
+
|
121
|
+
super.viewDidLoad()
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
var albumName = "MyCollection"
|
126
|
+
|
127
|
+
var assetCollection = PHAssetCollection()
|
128
|
+
|
129
|
+
var albumFound = Bool()
|
130
|
+
|
131
|
+
var photoAssets = PHFetchResult<AnyObject>()
|
132
|
+
|
133
|
+
let fetchOptions = PHFetchOptions()
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
|
138
|
+
|
139
|
+
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
if let first_Obj:AnyObject = collection.firstObject{
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
assetCollection = collection.firstObject as! PHAssetCollection
|
148
|
+
|
149
|
+
albumFound = true
|
150
|
+
|
151
|
+
print("アルバムみつかった")
|
152
|
+
|
153
|
+
}else {
|
154
|
+
|
155
|
+
albumFound = false
|
156
|
+
|
157
|
+
print("アルバムみつからない")
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
var i = collection.count
|
164
|
+
|
165
|
+
photoAssets = PHAsset.fetchAssets(in: assetCollection, options: nil) as! PHFetchResult<AnyObject>
|
166
|
+
|
167
|
+
let imageManager = PHCachingImageManager()
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
photoAssets.enumerateObjects({(object: AnyObject!,
|
172
|
+
|
173
|
+
count: Int,
|
174
|
+
|
175
|
+
stop: UnsafeMutablePointer<ObjCBool>) in
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
if object is PHAsset{
|
180
|
+
|
181
|
+
let asset = object as! PHAsset
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
let imageSize = CGSize(width: asset.pixelWidth,
|
186
|
+
|
187
|
+
height: asset.pixelHeight)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
let options = PHImageRequestOptions()
|
192
|
+
|
193
|
+
options.deliveryMode = .fastFormat
|
194
|
+
|
195
|
+
options.isSynchronous = true
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
imageManager.requestImage(
|
200
|
+
|
201
|
+
for: asset,
|
202
|
+
|
203
|
+
targetSize: imageSize,
|
204
|
+
|
205
|
+
contentMode: .aspectFill,
|
206
|
+
|
207
|
+
options: options,
|
208
|
+
|
209
|
+
resultHandler: {(image, info) -> Void in
|
210
|
+
|
211
|
+
//CollectionView用に配列に入れる!
|
212
|
+
|
213
|
+
self.photosImg = [image!]
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
)
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
})
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
// Cell はストーリーボードで設定したセルのID
|
244
|
+
|
245
|
+
let testCell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
// Tag番号を使ってImageViewのインスタンス生成
|
250
|
+
|
251
|
+
let imageView = testCell.contentView.viewWithTag(1) as! UIImageView
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// 画像配列の番号で指定された要素の名前の画像をUIImageとする
|
256
|
+
|
257
|
+
//cellImage = UIImage(named: photosImg[(indexPath as NSIndexPath).row])
|
258
|
+
|
259
|
+
cellImage = photosImg[(indexPath as NSIndexPath).row]
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
// UIImageをUIImageViewのimageとして設定
|
264
|
+
|
265
|
+
imageView.image = cellImage
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
// Tag番号を使ってLabelのインスタンス生成
|
270
|
+
|
271
|
+
let label = testCell.contentView.viewWithTag(2) as! UILabel
|
272
|
+
|
273
|
+
label.text = "a"
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
return testCell
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
// section数は1つ
|
288
|
+
|
289
|
+
return 1
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
296
|
+
|
297
|
+
// 要素数を入れる、要素以上の数字を入れると表示でエラーとなる
|
298
|
+
|
299
|
+
return photosImg.count;
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
// Screenサイズに応じたセルサイズを返す
|
306
|
+
|
307
|
+
// UICollectionViewDelegateFlowLayoutの設定が必要
|
308
|
+
|
309
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
310
|
+
|
311
|
+
let cellSize:CGFloat = self.view.frame.size.width/2-2
|
312
|
+
|
313
|
+
// 正方形で返すためにwidth,heightを同じにする
|
314
|
+
|
315
|
+
return CGSize(width: cellSize, height: cellSize)
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
```
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
|
326
|
+
|
73
327
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
74
328
|
|
75
329
|
Xcode 8.3
|
1
ソース
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,6 +42,32 @@
|
|
42
42
|
|
43
43
|
}
|
44
44
|
|
45
|
+
|
46
|
+
|
47
|
+
・・・・・・・・
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
let manager: PHImageManager = PHImageManager()
|
52
|
+
|
53
|
+
manager.requestImageForAsset(asset,
|
54
|
+
|
55
|
+
targetSize: CGSizeMake(70, 70),
|
56
|
+
|
57
|
+
contentMode: .AspectFill,
|
58
|
+
|
59
|
+
options: nil) { (image, info) -> Void in
|
60
|
+
|
61
|
+
imageView.image = image
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
45
71
|
```
|
46
72
|
|
47
73
|
###補足情報(言語/FW/ツール等のバージョンなど)
|