loadView 比通过 Assets 获取图像更快
因此,在 init 函数中,我通过 AssetsLibrary
//initWithNibName:
photoArray = [[NSMutableArray alloc ]init];
ALAssetsLibrary *asset = [[ALAssetsLibrary alloc] init];
void (^enumerateGroup)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result != nil) {
[photoArray addObject:result];
NSLog(@"%@", result);
}
};
void (^enumerationBlock)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group != nil) {
[group enumerateAssetsUsingBlock:enumerateGroup];
}
};
[asset enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:enumerationBlock
failureBlock:^(NSError *error) {NSLog(@"Something went wrong");}];
[asset release];
//loadView
- (void)loadView
{
UIView *view = [[UIView alloc ] init];
NSLog(@"%d", [photoArray count]);
self.view = view;
[view release];
}
Log from console:
2011-06-24 18:55:12.255 xxx[9450:207] 0 //
2011-06-24 18:55: 12.306 xxx[9450:207] ALAsset - 类型:照片,网址:{ "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000001&ext=JPG";
我很困惑。正如您在日志中看到的,loadView 执行代码的速度比 initWithNibName 快。这是因为通过 AssetLibrary 获取图像需要一些时间。但我认为所有这些代码都在一个线程中执行,因此 loadView 应该等待 initWithNibName。
So, in init function I am getting images via AssetsLibrary
//initWithNibName:
photoArray = [[NSMutableArray alloc ]init];
ALAssetsLibrary *asset = [[ALAssetsLibrary alloc] init];
void (^enumerateGroup)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result != nil) {
[photoArray addObject:result];
NSLog(@"%@", result);
}
};
void (^enumerationBlock)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group != nil) {
[group enumerateAssetsUsingBlock:enumerateGroup];
}
};
[asset enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:enumerationBlock
failureBlock:^(NSError *error) {NSLog(@"Something went wrong");}];
[asset release];
//loadView
- (void)loadView
{
UIView *view = [[UIView alloc ] init];
NSLog(@"%d", [photoArray count]);
self.view = view;
[view release];
}
Log from console:
2011-06-24 18:55:12.255 xxx[9450:207] 0 //
2011-06-24 18:55:12.306 xxx[9450:207] ALAsset - Type:Photo, URLs:{
"public.jpeg" = "assets-library://asset/asset.JPG?id=1000000001&ext=JPG";
And I am confused. As you can see in the log, loadView executed code faster than initWithNibName. This is because getting images via AssetLibrary take some time. But I think all of this code is executing in one thread, so loadView should wait, for initWithNibName.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-enumerateGroupsWithTypes:...
和-enumerateAssetsUsingBlock:...
的文档并没有说这些方法是同步执行的。从您发现的情况来看,他们似乎在不同的线程上进行枚举,因此您不必等待。The documentation for
-enumerateGroupsWithTypes:...
and-enumerateAssetsUsingBlock:...
doesn't say that those methods execute synchronously. From what you've found, it looks like they do their enumeration on a different thread so that you don't have to wait.