使用 ALAssetsLibrary 访问单个图像

发布于 2024-11-15 06:45:43 字数 148 浏览 4 评论 0原文

请允许我先说这是我第一次使用 ALAssetsLibrary。我需要访问用户保存的照片库中的最新照片。看来要做到这一点,我必须创建一个 ALAssetsLibrary 实例,并在选择最后一个图像之前迭代用户图库中的每个项目。这始终是最坏的情况。有没有更快/更好的方法来解决这个问题?

Allow me to preface this by saying this is my first time using the ALAssetsLibrary. I need to access the most recent photo in the user's saved photo gallery. It seems that to do this, I have to create an ALAssetsLibrary instance and iterate over every item in the user's gallery before selecting the last image. This is always worst-case scenario. Is there a faster/better way to approach this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法少女 2024-11-22 06:45:43

您不必枚举用户图库中的所有照片。
ALAssetsGroup 类有一个方法 - (void)enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock,您可以使用该方法来指示要枚举的资产。

在您的情况下,它只是最后一个,因此将 indexSet 设置为 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange([group numberOfAssets]-1, [group numberOfAssets]) 其中 group 是您的 ALAssetsGroup。

正如 @mithuntnt 提到的,您可以使用 [[assetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) 获取照片库的 ALAssetsGroup

You don't have to enumerate all the photos in the user's gallery.
The ALAssetsGroup class has a method - (void)enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock which you can use to indicate which assets you want to enumerate.

In your case it's only the last one so set indexSet to [NSIndexSet indexSetWithIndexesInRange:NSMakeRange([group numberOfAssets]-1, [group numberOfAssets]) where group is your ALAssetsGroup.

As @mithuntnt mentioned, you can get the ALAssetsGroup for the photo library using [[assetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)

櫻之舞 2024-11-22 06:45:43

这个怎么样:

[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
    if (result) {
       *stop = YES;
       //...
    }
}];

What about this:

[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
    if (result) {
       *stop = YES;
       //...
    }
}];
眸中客 2024-11-22 06:45:43

http://developer.apple.com/ Library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html

只有一种枚举方法。所以这是唯一的办法。

我需要最后导入的照片。您可以有一些与此类似的过滤器。

[[assetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if( group )
    {
        NSString * groupName = [group valueForProperty:ALAssetsGroupPropertyName];

        if( [@"Last Import" isEqualToString:groupName] )
        {
            *stop = true;

...

http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html

There is only one enumeration method. So this is the only way.

I needed the last imported photos. You can have some filter similar to this.

[[assetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if( group )
    {
        NSString * groupName = [group valueForProperty:ALAssetsGroupPropertyName];

        if( [@"Last Import" isEqualToString:groupName] )
        {
            *stop = true;

...

两个我 2024-11-22 06:45:43

如果您枚举已设置过滤器的 ALAssetGroup ,则接受的答案似乎不起作用(因为 [group numberOfAssets] 返回总资产而不是过滤后的总资产)。

我使用了这个:

typedef void(^SMKMostRecentPhotoCompletionBlock)(ALAsset *asset);

- (void)mostRecentPhotoWithCompletionBlock:(SMKMostRecentPhotoCompletionBlock)completionBlock
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    __block ALAsset *mostRecentPhoto = nil;

    if (group)
    {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

            if (result != NULL)
            {
                mostRecentPhoto = result;
                *stop = YES;
            }

        }];
    }

    if (completionBlock)
    {
        completionBlock(mostRecentPhoto);
    }

} failureBlock:^(NSError *error) {

    if (completionBlock)
    {
        completionBlock(nil);
    }

}];

}

在您的 completionBlock 中,确保检查返回的 ALAsset != nil。

The accepted answer doesn't appear to work if you're enumerating an ALAssetGroup that you've set a filter on (because [group numberOfAssets] returns the total assets rather then the total assets after filtering).

I used this:

typedef void(^SMKMostRecentPhotoCompletionBlock)(ALAsset *asset);

- (void)mostRecentPhotoWithCompletionBlock:(SMKMostRecentPhotoCompletionBlock)completionBlock
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    __block ALAsset *mostRecentPhoto = nil;

    if (group)
    {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

            if (result != NULL)
            {
                mostRecentPhoto = result;
                *stop = YES;
            }

        }];
    }

    if (completionBlock)
    {
        completionBlock(mostRecentPhoto);
    }

} failureBlock:^(NSError *error) {

    if (completionBlock)
    {
        completionBlock(nil);
    }

}];

}

In your completionBlock, make sure to check that the returned ALAsset != nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文