从文件异步加载图像 [英] Load image asynchronously from file

查看:119
本文介绍了从文件异步加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地存储的图像比较,我想展示给用户,而不会干扰用户界面线程。
我目前使用

I have an relatively image in local storage, I want to show it to the user without disturbing UI thread. I'm currently using

[[UIImage alloc] initWithContentsOfFile:path];

加载图像。

任何建议/帮助,请....

Any suggestions/help please....

推荐答案

如果你正在试图做的一切就是保持UI线程可用,成立了一个简短的方法来加载它在后台更新ImageView的完成时:

If all you're trying to do is keep the UI thread available, set up a short method to load it in the background and update the imageView when done:

-(void)backgroundLoadImageFromPath:(NSString*)path {
    UIImage *newImage = [UIImage imageWithContentsOfFile:path];
    [myImageView performSelectorOnMainThread:@selector(setImage:) withObject:newImage waitUntilDone:YES];
}

这presumes myImageView 是类的成员变量。现在,只需在任何线程在后台运行它:

This presumes myImageView is a member variable of the class. Now, simply run it in the background from any thread:

[self performSelectorInBackground:@selector(backgroundLoadImageFromPath:) withObject:path];

请注意,在 backgroundLoadImageFromPath 则需要等到 setImage:选择完成,否则后台线程的自动释放池在 setImage之前,可能释放图像:方法可以保留它

Note, in backgroundLoadImageFromPath you need to wait until the setImage: selector finishes, otherwise the background thread's autorelease pool may deallocate the image before the setImage: method can retain it.

这篇关于从文件异步加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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