dataWithContentsOfURL 和 imageWithData 占整个加载时间的 84% [英] dataWithContentsOfURL and imageWithData take 84% of the whole loading time

查看:85
本文介绍了dataWithContentsOfURL 和 imageWithData 占整个加载时间的 84%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两行占用了我的应用程序整个加载时间的 40% 和 42%(总共 84%).我使用Instruments 对其进行了测试.

these two lines took 40% and 42% (together 84%) of the whole loading time of my app. I tested it with Instruments.

NSData *storeImageData = [NSData dataWithContentsOfURL:storeImageURL]; //40% whole load time
UIImage *storeImage = [UIImage imageWithData:storeImageData]; //42% whole load time 

还有其他/更好的方法来加快我的应用程序的加载时间吗?这两行和更多代码都在一个循环中,大约会循环 500 次.

Is there another / better way to speed up the loading time of my app? These two lines and a lot more code are in a loop wich will loop about 500 times.

注意
将http://"添加到通常的www.blah.net"后,它开始变慢.有谁知道为什么 URL 中的 7 个字符(大约 30-50 个)会大大减慢加载时间.在我改变它之前,花了3秒钟.现在是 37 秒.

Note
after adding "http://" to the usual "www.blah.net" it starts to be slow. Does anyone know why 7 characters (of about 30-50) in an URL slows the loading time so massively down. Before I changed it, it took 3 seconds. Now 37 seconds.

推荐答案

用这些替换你的行,

 __block NSData *storeImageData;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL);
dispatch_async(queue, ^{
    //load url image into NSData
    storeImageData = [NSData dataWithContentsOfURL:storeImageURL];

    dispatch_sync(dispatch_get_main_queue(), ^{
        //convert data into image after completion
        UIImage *storeImage = [UIImage imageWithData:storeImageData];
        //do what you want to do with your image
    });

});
dispatch_release(queue);

有关更多信息,请参阅dispatch_queue_t

这篇关于dataWithContentsOfURL 和 imageWithData 占整个加载时间的 84%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆