2016년 8월 29일 월요일

PHAssetCollection 을 이용한 PhotoLibrary 앨범 정보 로드 @@ in Swift2.x - Xcode 7.3 iOS 9.3


PhotoLibrary 의 앨범 Data Load

*PHAssetCollection.fetchAssetCollectionsWithType 의 파라미터 값 설명참조
let getAlbums : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: options)



모바일에 저장된 사진앨범들의 정보(타이틀이름, 이미지파일)를 로드
사진앨범의 타이틀과 사진 수를 가져오는데 PHAssetCollection 사용하였습니다.

func GetAlbums() {
  let options:PHFetchOptions = PHFetchOptions()
  let getAlbums : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: options)
  // 앨범 정보
  print(getAlbums)
  for i in 0 ..< getAlbums.count{
    let collection = getAlbums[i]
    // . localizedTitle = 앨범 타이틀
    let title : String = collection.localizedTitle
      if(collection.estimatedAssetCount != nil){
        // . estimatedAssetCount = 앨범 내 사진 수
        let count : Int = collection.estimatedAssetCount
        print(count)
        print(title)
      }else{
      }
   }
}


위 코딩을 실행하면 자신이 추가한 앨범들의 타이틀과 사진 장수를 확인 할 수 있다
위 코드를 실행하면 앨범들의 타이틀 정보는 제대로 가져오나 사진 수를 가져오는데 
잘못된 값을 가져오는것을 확인 했다.

<앨범 내 사진 수를 가져오는 포스팅은 아래 링크를 참고>
https://swifteyes.blogspot.kr/2016/08/grid-poster-2-phassets-class-2-in.html


*기본 라이브러리 앨범들의 정보를 확인은 .Album -> .SamrtAlbum 으로 수정하면 앨범 타이틀은
출력이 되나 필요없는 앨범 타이틀까지 출력 되며 사진 수가 알 수 없는 숫자들로  "9223xx20368xx5807" 동일하게 나타난다. 



<subtype 을 이용해 자신이 원하는 앨범선택이 가능하다>


case 
albumRegular

An album created in the Photos app.
case albumSyncedEvent
An Event synced to the device from iPhoto.
case albumSyncedFaces
A Faces group synced to the device from iPhoto.
case albumSyncedAlbum
An album synced to the device from iPhoto.
case albumImported
An album imported from a camera or external storage.
case albumMyPhotoStream
The user’s personal iCloud Photo Stream.
case albumCloudShared
An iCloud Shared Photo Stream.
case smartAlbumGeneric
A smart album of no more specific subtype.
case smartAlbumPanoramas
A smart album that groups all panorama photos in the photo library.
case smartAlbumVideos
A smart album that groups all video assets in the photo library.
case smartAlbumFavorites
A smart album that groups all assets that the user has marked as favorites.
case smartAlbumTimelapses
A smart album that groups all time-lapse videos in the photo library.
case smartAlbumAllHidden
A smart album that groups all assets hidden from the Moments view in the Photos app.
case smartAlbumRecentlyAdded
A smart album that groups assets that were recently added to the photo library.
case smartAlbumBursts
A smart album that groups all burst photo sequences in the photo library.
case smartAlbumSlomoVideos
A smart album that groups all Slow-Mo videos in the photo library.
case smartAlbumUserLibrary
A smart album that groups all assets that originate in the user’s own library (as opposed to assets from iCloud Shared Albums).
case smartAlbumSelfPortraits
A smart album that groups all photos and videos captured using the device’s front-facing camera.
case smartAlbumScreenshots
A smart album that groups all images captured using the device’s screenshot function.

case any
A bit mask representing all possible subtypes.



options 파라미터 값을 이용해 앨범에서 이미지 타입의 파일만 가져오기

let options = PHFetchOptions()
// 옵션 값 설정 : 이미지 타입
options.predicate = NSPredicate(format: "mediaType = %d",PHAssetMediaType.Image.rawValue)
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]
let result : PHFetchResult = PHAsset.fetchAssetsWithOptions(options)
  for i in 0 ..< result.count {
    let asset = result[i] as! PHAsset
    print("date = \(asset.creationDate!)")
    print("location = \(asset.location)")
    print("width = \(asset.pixelWidth), height=\(asset.pixelHeight)")
  }

options.predicate 값에 이미지타입의 파일만 가져오도록 할 수 있다.

특정앨범 선택하기

특정 앨범을 선택할땐 아래처럼 format을 title로하여 앨범 이름 값을 넣어주면 된다

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", named)


<특정 앨범의 정보를 가져오는 내용을 아래 링크를 참조>
https://swifteyes.blogspot.kr/2016/08/grid-poster-3-phassets-class-3-in.html



댓글 없음:

댓글 쓰기

추천 게시물

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

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