특정앨범 정보 로드
앨범 리스트 가져올때 PHAssetCollection 의 subtype 값을 설정함으로써 자신이 원하는 앨범
정보를 가져올 수 있다.
<참조>let Customalbums = PHAssetCollection.fetchAssetCollectionsWithType(. SmartAlbum, subtype: .Any, options: fetchOptions)
<subtype 정리>
let Customalbums = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
let Customalbums = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
// 사용자가 만든 앨범
let SmartAlbumPanoramas = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumPanoramas, options: fetchOptions)
let SmartAlbumFavorites = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumFavorites, options: fetchOptions)
let SmartAlbumSelfPortraits = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumSelfPortraits, options: fetchOptions)
let SmartAlbumScreenshots = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumScreenshots, options: fetchOptions)
let SmartAlbumBursts = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumBursts, options: fetchOptions)
let SmartAlbumRecentlyAdded = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumRecentlyAdded, options: fetchOptions)
let Cmeraroll = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: fetchOptions)
let SmartAlbumPanoramas = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumPanoramas, options: fetchOptions)
let SmartAlbumFavorites = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumFavorites, options: fetchOptions)
let SmartAlbumSelfPortraits = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumSelfPortraits, options: fetchOptions)
let SmartAlbumScreenshots = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumScreenshots, options: fetchOptions)
let SmartAlbumBursts = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumBursts, options: fetchOptions)
let SmartAlbumRecentlyAdded = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumRecentlyAdded, options: fetchOptions)
let Cmeraroll = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: fetchOptions)
앨범 값 을 album 이라는 상수에 저장하여 PHAssetCollection 의 localizedTitle 을 이용해 앨범 타이틀 이름 로드
let albumTitle : String = album.localizedTitle!fetchAssetsInAssetCollection 의 count 을 이용해 앨범 사진 수 로드
let assetsFetchResult: PHFetchResult = PHAsset.fetchAssetsInAssetCollection(album, options: nil)
let albumCount = assetsFetchResult.count
options 값을 설정하여 이미지파일만 가져오기
let fetchOptions2 = PHFetchOptions()fetchOptions2.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)
<완성된 예제 코드>
// 앨범리스트 담을 변수 설정
var albumList:[AlbumModel] = [AlbumModel]()
// 옵션 값
let fetchOptions = PHFetchOptions()
//fetchAssetCollectionsWithType의 subtype 타입값을 설정하여 가지고 오고 싶은 앨범만 선택
let Customalbums = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
let SmartAlbumPanoramas = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumPanoramas, options: fetchOptions)
let SmartAlbumFavorites = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumFavorites, options: fetchOptions)
let SmartAlbumSelfPortraits = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumSelfPortraits, options: fetchOptions)
let SmartAlbumScreenshots = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumScreenshots, options: fetchOptions)
let SmartAlbumBursts = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumBursts, options: fetchOptions)
let SmartAlbumRecentlyAdded = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumRecentlyAdded, options: fetchOptions)
let Cmeraroll = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: fetchOptions)
// 각 앨범의 사진 타이틀이름, 수 가져오기
[Cmeraroll, SmartAlbumRecentlyAdded, SmartAlbumSelfPortraits, SmartAlbumFavorites, SmartAlbumBursts, SmartAlbumPanoramas, SmartAlbumScreenshots, Customalbums].forEach {
$0.enumerateObjectsUsingBlock { collection, index, stop in guard let album = collection as? PHAssetCollection else { return }
// PHAssetCollection 의 localizedTitle 을 이용해 앨범 타이틀 가져오기
let albumTitle : String = album.localizedTitle!
// 이미지만 가져오도록 옵션 설정
let fetchOptions2 = PHFetchOptions()
fetchOptions2.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)
let assetsFetchResult: PHFetchResult = PHAsset.fetchAssetsInAssetCollection(album, options: fetchOptions2)
// PHFetchResult 의 count 을 이용해 앨범 사진 갯수 가져오기
let albumCount = assetsFetchResult.count
// 저장
let newAlbum = AlbumModel(name:albumTitle, count: albumCount, collection:album)
print(newAlbum.name)
print(newAlbum.count)
//앨범 정보 추가
albumList.append(newAlbum)
albumList.append(newAlbum)
}
}
class AlbumModel {
let name:String
let count:Int
let collection:PHAssetCollection
init(name:String, count:Int, collection:PHAssetCollection) {
self.name = name
self.count = count
self.collection = collection
}
}
<참조>
enumerateObjectsUsingBlock 사용
https://developer.apple.com/reference/photos/phfetchresult/1620998-enumerateobjects
class AlbumModel {
let name:String
let count:Int
let collection:PHAssetCollection
init(name:String, count:Int, collection:PHAssetCollection) {
self.name = name
self.count = count
self.collection = collection
}
}
<참조>
enumerateObjectsUsingBlock 사용
https://developer.apple.com/reference/photos/phfetchresult/1620998-enumerateobjects