iphone - ALAssetsLibrary 太慢 - Objective-C
<p><p>从相机胶卷中加载 10-20 张全屏图像、保存照片的快速方法是什么?</p>
<p>我正在使用此代码,但要加载 10 张照片,我需要等待大约 5-10 秒。我用的是 iPhone 4S。</p>
<pre><code>ALAssetsLibrary *library = [ init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(_savedPhotos.count>=11) *stop = YES;
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
NSLog(@"%d",index);
if(_savedPhotos.count<11)
{
UIImage *image = ;
;
}
else
{
*needToStop = YES;
}
}];
} failureBlock:^(NSError *error) {
NSLog(@"%@",error.description);
}];
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>ALAssetsLibrary</strong> 库将在<em>单独的</em> 线程上运行。因此,与 <a href="http://en.wiktionary.org/wiki/UI#Initialism" rel="noreferrer noopener nofollow">UI</a> 进行通信可能需要一些时间。相关的和其他的东西。</p>
<p>所以在 ALAssetsLibraryblock 中使用 <strong><code>-performSelectorOnMainThread:withObject:waitUntilDone:</code></strong>。</p>
<p><em><strong>如下更改您的代码</strong></em></p>
<pre><code>ALAssetsLibrary *library = [ init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
NSLog(@"%d",index);
UIImage *image = ;
;
}];
}
failureBlock:^(NSError *error) {
NSLog(@"%@",error.description);
}];
- (void)usePhotolibraryimage:(UiImage *)myImage{
//Do your all UI related and all stuff here
}
</code></pre>
<p><strong>注意</strong>:看<a href="http://www.daveoncode.com/2011/10/15/solve-xcode-error-invalid-attempt-to-access-alassetprivate-past-the-lifetime-of-its-owning-alassetslibrary/" rel="noreferrer noopener nofollow">this issue too</a> .</p></p>
<p style="font-size: 20px;">关于iphone - ALAssetsLibrary 太慢 - Objective-C,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/13008251/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/13008251/
</a>
</p>
页:
[1]