问题描述
我在本地存储的图像比较,我想展示给用户,而不会干扰用户界面线程。
我目前使用
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.
这篇关于从文件异步加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!