回答編集履歴
1
サンプルコードを追記
answer
CHANGED
@@ -7,4 +7,43 @@
|
|
7
7
|
|
8
8
|
> let assets = PHAsset.fetchAssets(in:, options:)
|
9
9
|
|
10
|
-
あとは質問文に書いてある内容と同じ方法でassetから画像が取れます。
|
10
|
+
あとは質問文に書いてある内容と同じ方法でassetから画像が取れます。
|
11
|
+
|
12
|
+
---
|
13
|
+
2018/06/27追記
|
14
|
+
|
15
|
+
サンプルを載せておきます。
|
16
|
+
エラーチェック等は省いていますので、ご了承ください。
|
17
|
+
|
18
|
+
```swift
|
19
|
+
let imgManager = PHImageManager.default()
|
20
|
+
|
21
|
+
let requestOptions = PHImageRequestOptions()
|
22
|
+
requestOptions.isSynchronous = true
|
23
|
+
requestOptions.deliveryMode = .highQualityFormat
|
24
|
+
|
25
|
+
let fetchOptions = PHFetchOptions()
|
26
|
+
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
|
27
|
+
|
28
|
+
// アルバムをフェッチ
|
29
|
+
let assetCollections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)
|
30
|
+
|
31
|
+
assetCollections.enumerateObjects { assetCollection, _, _ in
|
32
|
+
|
33
|
+
// アルバムタイトル
|
34
|
+
print(assetCollection.localizedTitle ?? "")
|
35
|
+
|
36
|
+
// アセットをフェッチ
|
37
|
+
let assets = PHAsset.fetchAssets(in: assetCollection, options: fetchOptions)
|
38
|
+
|
39
|
+
assets.enumerateObjects { asset, _, _ in
|
40
|
+
// 画像のリクエスト
|
41
|
+
imgManager.requestImage(for: asset, targetSize: CGSize(width: 200, height: 200), contentMode:
|
42
|
+
.aspectFill, options: requestOptions, resultHandler: { img, _ in
|
43
|
+
if let img = img {
|
44
|
+
print("画像の取得に成功")
|
45
|
+
}
|
46
|
+
})
|
47
|
+
}
|
48
|
+
}
|
49
|
+
```
|