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년 8월 30일 화요일

fetchAssetsInAssetCollection 으로 앨범 내 사진 수 가져오기 @@ in Swift2.x - Xcode 7.3 iOS 9.3



PHAssetCollection 이용하여 모바일 내 앨범정보를 가져와 타이틀과 사진 장수를 가져오려 했으나
PHAssetCollection 의 메타데이터 estimatedAssetCount 를 사용하면 "9223xx20368xx5807" 메세지가 동일하게 나타나는 문제점이 있었다.

PHAsset 클래스의 fetchAssetsInAssetCollection 을 기존코딩에 추가하여 

사진 장수를 가져오는 해결책을 알아냈다.

fetchAssetsInAssetCollection(_ assetCollection: PHAssetCollection, options options: PHFetchOptions?) -> PHFetchResult


fetchAssetCollections 을 사용해서 PHFetchResult로 값을 가져올 경우 estimatedAssetCount 의 인자값을 출력 시 "9223xx20368xx5807" 메세지가 나오기 때문에 <참조>
let options:PHFetchOptions = PHFetchOptions()
let getAlbums: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: options)
https://developer.apple.com/reference/photos/phfetchresult



result 인자 값으로 count 가 있는fetchAssetsInAssetCollection 사용해 보기로 했다.

func GetAlbums() {
  let options:PHFetchOptions = PHFetchOptions()
  let getAlbums : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: options)
  // 앨범 정보
  print(getAlbums)
  for i in 0 ..< getAlbums.count{
    let assetCollection:PHAssetCollection = getAlbums[i] as! PHAssetCollection
    print(assetCollection.localizedTitle)

    print(assetCollection.estimatedAssetCount)
  // 위 글에서 특정앨범의 정보를 가져오는 fetchAssetsInAssetCollection 을 사용한다
  // PHFetchResult의 타입의 상수에 값을 저장한다
  // PHFetchResult 의 result 값에 count 가 있다

    let assetsFetchResult: PHFetchResult = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: nil)
  // 출력 시 기존 생성된 앨범들의 사진 및 비디오 수가 나옴
  print("assetsFetchResult.count=\(assetsFetchResult.count)")
  } 

}


앨범들의 사진 수를  (Count) 를 제대로 가져오는걸 확인 할 수 있다.






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 클릭 >> 무조건 승인!! ...