gpt4 book ai didi

ios8 - iOS 8 照片框架 : Get a list of all albums with iOS8

转载 作者:行者123 更新时间:2023-12-02 23:49:51 31 4
gpt4 key购买 nike

如何在 iOS8 中获取所有收藏的列表,包括相机胶卷(现在称为时刻)?

在 iOS 7 中,我使用 ALAssetGroup 枚举 block ,但这不包括 iOS 时刻,这似乎相当于 iOS7 中的相机胶卷。

    void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil) {// We're done enumerating
return;
}

[group setAssetsFilter:[ALAssetsFilter allAssets]];
if ([[sGroupPropertyName lowercaseString] isEqualToString:@"camera roll"] && nType == ALAssetsGroupSavedPhotos) {
[_assetGroups insertObject:group atIndex:0];
} else {
[_assetGroups addObject:group];
}
};

// Group Enumerator Failure Block
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
SMELog(@"Enumeration occured %@", [error description]);
};

// Enumerate Albums
[_library enumerateGroupsWithTypes:kSupportedALAlbumsMask
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];
}];

最佳答案

使用 Photos Framework 有点不同,但您可以实现相同的结果,只需分批进行即可。

1)获取所有照片(iOS8中的时刻,或之前的相机胶卷)

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];

如果您希望它们按创建日期排序,您只需添加 PHFetchOptions 即可,如下所示:

PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];

现在,如果您愿意,您可以从 PHFetchResult 对象获取资源:

[allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
NSLog(@"asset %@", asset);
}];

2) 获取所有用户相册(例如,通过附加排序仅显示至少包含一张照片的相册)

PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];

[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
NSLog(@"album title %@", collection.localizedTitle);
}];

对于从 PHFetchResult *userAlbums 返回的每个 PHAssetCollection,您可以像这样获取 PHAssets(您甚至可以限制结果仅包含照片) Assets ):

PHFetchOptions *onlyImagesOptions = [PHFetchOptions new];
onlyImagesOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:onlyImagesOptions];

3) 获取智能相册

PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
NSLog(@"album title %@", collection.localizedTitle);
}];

关于智能相册需要注意的一件事是,如果无法确定estimatedAssetCount,collection.estimatedAssetCount 可能会返回NSNotFound。正如标题所示,这是估计的。如果您想确定 Assets 的数量,您必须执行获取并获取计数,例如:

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

Assets 数量 = assetsFetchResult.count

Apple 有一个示例项目可以满足您的需求:

https://developer.apple.com/library/content/samplecode/UsingPhotosFramework/ExampleappusingPhotosframework.zip (您必须是注册开发者才能访问此内容)

关于ios8 - iOS 8 照片框架 : Get a list of all albums with iOS8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25981374/

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