인디노트

iOS PhotoKit - 카메라 롤과 일반 앨범 본문

소스 팁/Objective C, Swift, iOS, macOS

iOS PhotoKit - 카메라 롤과 일반 앨범

인디개발자 2017. 12. 31. 21:30

출처: http://baccusf.tistory.com/45?category=209579 [Tomorrow is better than now]



카메라 롤과 일반 앨범 가져오기


PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum                                             subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary
   options:nil];

PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];

"[PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil]"의 경우 애플의 문서를 보면 
"Retrieves collections from the root of the photo library’s hierarchy of user-created albums and folders."라고 씌여있다.


여기서 각 Asset을 가져오려면 아래와 같이 코드를 작성한다.

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];

만들어진 날짜를 가지고 정렬할 경우 options에 nil이 아닌 다음과 같이 코드를 작성한다.


PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

맨 위의 PHFetchResult 두 가지를 합하면 카메라 롤과 사용자가 만든 일반 앨범 리스트가 되고,  그 안에 있는 PHAssetCollection 를 PHAsset 클래스의 fetchAssetsInAssetCollection 메소드를 활용하면 PHAsset"을 꺼내올 수 있다.




카메라 롤과 일반 앨범 분류


<일반 앨범>

PHFetchResult *folderLists = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum

                                                                               subtype:PHAssetCollectionSubtypeAlbumRegular

                                                                               options:nil];


folderLists를 enumerateObjectsUsingBlock로 돌려보면 앨범에 대한 정보가 확인된다.


fetchAssetCollectionsWithType에서 PHAssetCollectionTypeAlbum가 일밤 앨범을 의미하고, 

PHAssetCollectionTypeSmartAlbum은 카메라롤을 포함한, 8.0에서 새로 추가된 파노라마, 슬로우 모션 등을 의미한다. 


PHAssetCollectionTypeSmartAlbum에서 또 다시 카메라롤만 분류할 때는 PHAssetCollectionSubtypeSmartAlbumUserLibrary를 사용하면 된다.


<카메라 롤>

PHFetchResult *smartFolderLists = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum

                                                                          subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary

                                                                          options:nil];

     

[smartFolderLists enumerateObjectsUsingBlock:^(PHAssetCollection *smartFolderAssetCollection, NSUInteger idx, BOOL *stop) { 

PHFetchResult  *assets = [PHAsset fetchAssetsInAssetCollection:smartFolderAssetCollection  options:nil];

//        [assets  enumerateObjectsUsingBlock : ^(PHAsset  *asset, NSUInteger idx, BOOL *stop) {

//                NSLog (@"asset : %@" , asset);

//        }];

}];


애플의 샘플 예제는  SAMPLEPHOTOSAPP.ZIP 에서 확인한다.


반응형
Comments