[iOS 8] PhotoKit 2 – Photos Framework – モデルオブジェクトの取得

2014.09.18

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

今回はPhotos Frameworkのモデルオブジェクトを取得をしてみます。

モデルオブジェクトをフェッチするには、モデルオブジェクトのクラスメソッドを使用します。

モデルオブジェクトの継承関係

Photos Frameworkのモデルオブジェクトの継承関係は以下のとおりです。

  • PHObject (ベースクラス)
    • PHAsset
    • PHCollection
      • PHAssetCollection
      • PHCollectionList

Photos Framework を使う準備

@importをヘッダファイルまたは実装ファイルに追加します。

@import Photos;

取得例

PHCollectionList、PHAssetCollection、PHAssetの順にモデルオブジェクトを取得してみます。

1. PHCollectionListの取得

まずはfetchCollectionListsWithType:subtype:options:メソッドを使用してPHAssetCollectionを取得します。 このメソッドではタイプとオプションを指定してPHCollectionListを取得できます。

// type: PHCollectionListTypeMomentList
// subtype: PHCollectionListSubtypeAny
PHFetchResult *momentLists = [PHCollectionList fetchCollectionListsWithType:PHCollectionListTypeMomentList subtype:PHCollectionListSubtypeAny options:nil];

// type: PHCollectionListTypeFolder
// subtype: PHCollectionListSubtypeAny
PHFetchResult *folderLists = [PHCollectionList fetchCollectionListsWithType:PHCollectionListTypeFolder subtype:PHCollectionListSubtypeAny options:nil];

// type: PHCollectionListTypeSmartFolder
// subtype: PHCollectionListSubtypeAny
PHFetchResult *smartFolderList = [PHCollectionList fetchCollectionListsWithType:PHCollectionListTypeSmartFolder subtype:PHCollectionListSubtypeAny options:nil];

FetchResultについて

fetchCollectionListsWithType:メソッドはPHFetchResultを返却します。(他のモデルオブジェクトの取得メソッドも同様です)

結果がある場合は、enumerateObjectsUsingBlock:メソッド等で取り出せます。

[momentLists enumerateObjectsUsingBlock:^(PHCollectionList *momentCollectionList, NSUInteger idx, BOOL *stop) {
        NSLog(@"momentCollectionList:%@", momentCollectionList);
}];

オプションについて

モデルオブジェクト取得用のクラスメソッドにはオプション(PHFetchOptions)を指定できるようになっています。

NSDate *toDate = [NSDate date];
NSDate *fromDate = [toDate dateByAddingTimeInterval:-(60*60*24)*60];

PHFetchOptions *fetchOptions = [PHFetchOptions new];
// predicate
fetchOptions.predicate = [NSPredicate predicateWithFormat: @"(startDate >= %@ ) and (endDate < %@)", fromDate, toDate];
// sortDescriptors
fetchOptions.sortDescriptors = @[
                                     [NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:NO],
                                     ];
PHFetchResult *momentList = [PHCollectionList fetchCollectionListsWithType:PHCollectionListTypeMomentList
                                                                   subtype:PHCollectionListSubtypeAny
                                                                   options:fetchOptions];
[/oc]


<h3 id="2-phassetcollection-">2. PHAssetCollectionの取得</h3>
<p>PHCollectionListのクラスメソッドで取得したmoment list(PHCollectionList)からPHAssetCollectionを取得してみます。</p>

// PHAssetCollectionを取得
PHFetchResult *moments = [PHAssetCollection fetchMomentsInMomentList:momentCollectionList options:nil];
[moments enumerateObjectsUsingBlock:^(PHAssetCollection *momentAssetCollection, NSUInteger idx, BOOL *stop) {
    NSLog(@"momentAssetCollection:%@", momentAssetCollection);
}];

3. PHAssetの取得

PHAssetCollectionのクラスメソッドで取得したasset collectionからPHAssetを取得してみます。

// PHAssetを取得
PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:momentAssetCollection options:nil];
[assets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
    NSLog(@"asset:%@", asset);
}];

4. 画像の取得

Photos Frameworkのモデルオブジェクトはメタデータだけ保持しています。画像自体を取得するにはPHImageManagerクラスを使用します

[[PHImageManager defaultManager] requestImageForAsset:asset
                   targetSize:CGSizeMake(300,300)
                  contentMode:PHImageContentModeAspectFit
                      options:nil
                resultHandler:^(UIImage *result, NSDictionary *info) {
                    if (result) {
                        // _weakSelf.imageView.image = result;
                        // _weakSelf.label.text = [NSString stringWithFormat:@"UIImage:%@", NSStringFromCGSize(result.size)];
                        // _weakSelf.label2.text = [NSString stringWithFormat:@"imageView:%@", NSStringFromCGSize(self.imageView.frame.size)];
                    }
                }];

各モデルオブジェクトのメソッド

モデルオブジェクト取得時に使用できるその他のメソッドは以下の通りです。

PHCollectionList

メソッド名 内容
fetchCollectionListsWithType:subtype:options: タイプとオプションを指定してPHCollectionListを取得
fetchCollectionListsContainingCollection:options: 特定のPHCollectionを含むPHCollectionListを取得
fetchCollectionListsWithLocalIdentifiers:options: 特定のlocalIdentifierのPHCollectionListを取得
fetchMomentListsWithSubtype:containingMoment:options: 特定のmomentを含む特定のタイプのmoment list(PHCollectionList)を取得
fetchMomentListsWithSubtype:options: 特定のmomentを含むmoment list(PHCollectionList)を取得

PHCollection

メソッド名 内容
fetchTopLevelUserCollectionsWithOptions: ユーザーが作成したコレクション(アルバムやフォルダ)を取得
fetchCollectionsInCollectionList:options: 特定のPHCollectionListからPHCollectionを取得

PHAssetCollection

メソッド名 内容
fetchMomentsInMomentList:options: 特定のmoment list(PHCollectionList)からPHAssetCollectionを取得
fetchAssetCollectionsWithLocalIdentifiers:options: 特定のlocalIdentifierのPHAssetCollectionを取得
fetchAssetCollectionsWithType:subtype:options: 特定のtypeのPHAssetCollectionを取得
fetchAssetCollectionsContainingAsset:withType:options: 特定のPHAssetを含むPHAssetCollectionを取得
fetchAssetCollectionsWithALAssetGroupURLs:options: 特定のURL(Assets Library Frameworkから提供)のPHAssetCollectionを取得
fetchMomentsWithOptions: Photosアプリで表示されるモーメントに対応するPHAssetCollectionを取得

PHAsset

メソッド名 内容
fetchAssetsInAssetCollection:options: 特定のasset collection(PHAssetCollection)からPHAssetを取得
fetchAssetsWithMediaType:options: 特定のmedia typeのPHAssetを取得
fetchAssetsWithLocalIdentifiers:options: 特定のlocalIdentifierのPHAssetを取得
fetchKeyAssetsInAssetCollection:options: 特定のAssetCollection内の"key asset"を取得
fetchAssetsWithOptions: 全てのPHAssetを取得
fetchAssetsWithBurstIdentifier:options: 特定のburstIdentifierのPHAssetを取得
fetchAssetsWithALAssetURLs:options: 特定のURL(Assets Library Frameworkから提供)のPHAssetを取得

まとめ

今回はモデルオブジェクトの取得について説明しました。次回はモデルオブジェクトのプロパティの編集をやってみたいと思います。

参考資料

本シリーズの記事一覧