2016년 8월 31일 수요일

PHAssetCollection의 subtype으로 특정 앨범 정보 확인 @@ in Swift2.x - Xcode 7.3 iOS 9.3

특정앨범 정보 로드

앨범 리스트 가져올때 PHAssetCollection 의  subtype 값을 설정함으로써 자신이 원하는 앨범
정보를 가져올 수 있다.

<참조>
let Customalbums = PHAssetCollection.fetchAssetCollectionsWithType(. SmartAlbum, subtype: .Any, options: fetchOptions)

<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)




앨범 값 을 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)
    }
  }


  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
  }

}

댓글 없음:

댓글 쓰기

추천 게시물

애플 개발자 등록방법 2016년 5월 8일 기준!!

애플 개발자 등록 절차 1. 개발자 등록 페이지 이동    애플 개발자 로그인 > Account 페이지 이동 > 하단 영역 클릭 (이미지 참조)   >> Enroll 클릭 >> 무조건 승인!! ...