iPhone - 从 URL 加载图像时内容不会滚动
我正在尝试在我的 iPhone 应用程序中执行某些操作,从 URL 下载图像。图像在页面上下载得很好,但在图像下载之前我无法滚动浏览内容或以任何其他方式与其交互,这使得应用程序一开始感觉像是被冻结了。
我刚刚使用此代码来加载图像:
- (void)viewDidAppear:(BOOL)animated
{
*photo1.image = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.ikipond.com/wp-includes/images/cosplayipn/will_1.png"]
]
];
}
我非常感谢有关如何制作它的建议,以便用户可以在图像仍在下载时与页面交互。非常感谢,
克里斯
I'm trying to do something in my iPhone app where it downloads an image from a URL. The image downloads fine on the page, but I can't scroll through content or interact with it in any other way until the images download, making the app feels like it's frozen at first.
I've just used this code to load the image:
- (void)viewDidAppear:(BOOL)animated
{
*photo1.image = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.ikipond.com/wp-includes/images/cosplayipn/will_1.png"]
]
];
}
I'd really appreciate a suggestion of how to make it so users can interact with the page while the images are still downloading. Thanks a lot,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dataWithContentsOfURL:
方法会阻塞,直到下载完成,因此阻塞了主 (GUI) 线程。您需要异步下载图像(使用 NSURLConnection)或在后台线程中下载。The
dataWithContentsOfURL:
method blocks until the download is complete, and is thus blocking the main (GUI) thread. You need to download the image asynchronously (usingNSURLConnection
) or in a background thread.在除主线程之外的任何线程上执行此操作。
一个开始的地方; 异步图像下载器
do it on any thread but the main.
a place to start; Async image downloader