我一直在尝试寻找替代方案,以便在iOS 8中使用Photos API获取相册名称。使用ALAssets,我们可以使用:valueForProperty:ALAssetsGroupPropertyName
但是使用Photos API,我似乎无法找到替代。在localizedTitle
下有PHAssetCollection
,但这也不对,它只是给了我城市名称。我正在寻找可以返回群组实际名称的内容,包括与iTunes同步的内容。
如果您在应用中看到了这些操作,我们将不胜感激。 Apple鼓励我们仅将照片API用于与8.0链接的应用,因此我不想同时使用ALAssetLibrary和照片。
代码:
- (NSString *)nameForAlbumInCollection:(id)collection
{
NSString *title = nil;
if ([PHAsset class])
{
title = [collection localizedTitle];
}
else
{
title = [collection valueForProperty:ALAssetsGroupPropertyName];
}
return title;
}
- (void)setup
{
self.recentsCollectionDataSource = [[NSMutableOrderedSet alloc]init];
self.favoritesCollectionDataSource = [[NSMutableOrderedSet alloc]init];
self.albumsTableDataSource = [[NSMutableOrderedSet alloc]init];
NSMutableArray *segmentTitles = [[NSMutableArray alloc]init];
self.assetsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
if (!self.parentController.canTakeOrChooseVideo)
{
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i",PHAssetMediaTypeImage];
}
for (PHAssetCollection *sub in self.assetsFetchResult)
{
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil];
for (PHAsset *asset in assetsInCollection)
{
NSLog(@"%@",[self nameForAlbumInCollection:sub]);
[self.recentsCollectionDataSource addObject:asset];
if (![segmentTitles containsObject:@"Recents"])
{
[segmentTitles addObject:@"Recents"];
[segmentTitles addObject:@"Albums"];
}
if (asset.isFavorite)
{
[self.favoritesCollectionDataSource addObject:asset];
if (![segmentTitles containsObject:@"Favorites"])
{
[segmentTitles addObject:@"Favorites"];
}
}
}
}
}
答案 0 :(得分:10)
这就是我在我的项目中列出相册名称的方法。你可能不得不偏离一点,但这应该有效。
NSArray *collectionsFetchResults;
NSMutableArray *localizedTitles = [[NSMutableArray alloc] init];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHFetchResult *syncedAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAlbumSyncedAlbum options:nil];
PHFetchResult *userCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
// Add each PHFetchResult to the array
collectionsFetchResults = @[smartAlbums, userCollections, syncedAlbums];
for (int i = 0; i < collectionsFetchResults.count; i ++) {
PHFetchResult *fetchResult = collectionsFetchResults[i];
for (int x = 0; x < fetchResult.count; x ++) {
PHCollection *collection = fetchResult[x];
localizedTitles[x] = collection.localizedTitle;
}
}
答案 1 :(得分:0)
我认为您可能使用的是PHCollectionList的localizedLocationNames属性。您应该在问题中提到的PHAssetCollection下使用localizedTitle。
我写了一个小实用程序类,并添加了以下方法来检索专辑名称。
+ (NSString *)localizedTitleForGroupOrCollection:(id)collection {
NSString *title = nil;
if ([PHAsset class]) {
title = [collection localizedTitle];
} else {
title = [collection valueForProperty:ALAssetsGroupPropertyName];
}
return title;
}
答案 2 :(得分:0)
位掩码不能用作PHAssetCollectionTypeAlbum,PHAssetCollectionTypeSmartAlbum和PHAssetCollectionTypeMoment未指定为位字段。它们取值1,2,3。要使位掩码工作,它们必须取值1,2,4,每个值都在不同的位上。