我的UITableView中有若干UIButton,图片存放于本地硬盘,读取到内存后放进UIButton展示。
子线程负责从文件读入图片到内存,主线程用setImage:forState:展示。由于setImage:forState:耗时较长且在主线程,导致Table拖动起来较卡。代码如下(UIButton上的扩展):
- (void)asyncLoadImageAtPath:(NSString *)fullPath forState:(UIControlState)state
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fullPath];
dispatch_async(dispatch_get_main_queue(), ^{
[self setImage:image forState:state];
});
});
}
后将setImage:forState:移入子线程,卡是不卡了,但是setImage:forState线程不安全,setImage:forState调用两秒后才会被显示。代码改为:
- (void)asyncLoadImageAtPath:(NSString *)fullPath forState:(UIControlState)state
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fullPath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self setImage:image forState:state];
});
});
}
QQ电影票中存于本地的大图拖起来不卡,且看起来是线程安全的。不知是怎么做的?
解决:最后读图重画成小尺寸一个线程,渲染一个线程,性能和自带的Photos差不多