gpt4 book ai didi

ios - PHPhotoLibrary 获取相册名称

转载 作者:可可西里 更新时间:2023-11-01 05:05:18 25 4
gpt4 key购买 nike

我一直在尝试寻找一种替代方法,以在 iOS 8 中使用 Photos API 获取相册名称。使用 ALAssets,我们可以使用:valueForProperty:ALAssetsGroupPropertyName 但是,使用 Photos API,我可以'似乎找到了替代方案。有: PHAssetCollection 下的 localizedTitle 但这也不对,它只是给了我城市名称。我正在寻找可以返回组的实际名称的东西,包括与 iTunes 同步的名称。

如果能帮助我了解您如何在您的应用中执行此操作,我将不胜感激。 Apple 鼓励我们仅将 Photos API 用于与 8.0 相关联的应用程序,因此我不想同时使用 ALAssetLibrary 和 Photos。

代码:

- (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"];
}
}
}
}
}

最佳答案

这就是我在我的项目中制作专辑名称列表的方式。您可能需要稍微偏离一下,但这应该可行。

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;

}
}

关于ios - PHPhotoLibrary 获取相册名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30605837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com