使用 ALAssetsLibrary 访问单个图像
请允许我先说这是我第一次使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不必枚举用户图库中的所有照片。
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)
获取照片库的 ALAssetsGroupYou 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)
这个怎么样:
What about this:
http://developer.apple.com/ Library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html
只有一种枚举方法。所以这是唯一的办法。
我需要最后导入的照片。您可以有一些与此类似的过滤器。
...
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.
...
如果您枚举已设置过滤器的
ALAssetGroup
,则接受的答案似乎不起作用(因为[group numberOfAssets]
返回总资产而不是过滤后的总资产)。我使用了这个:
}
在您的
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:
}
In your
completionBlock
, make sure to check that the returned ALAsset != nil.