loadView 比通过 Assets 获取图像更快

发布于 2024-11-16 19:48:54 字数 1410 浏览 2 评论 0原文

因此,在 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 技术交流群。

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

发布评论

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

评论(1

深海不蓝 2024-11-23 19:48:54

-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.

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