使用场景:
当表情键盘使用for循环借助UI控件imageNamed:方法赋值图片的时候界面滑动初次加载时候出现卡顿
解决方案:
- (void)decodedImageWithImageName:(NSString *)imageName block:(void(^)(UIImage *image))block {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@autoreleasepool{
UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
image = [UIImage decodedImageWithImage:image];
dispatch_async(dispatch_get_main_queue(), ^{
if(block) {
block(image);
}
});
}
});
}
其中decodedImageWithImage:
为SDWebImage
的代码,需要在响应文档的.m中#import "SDWebImageDecoder.h"
使用案例:
for (int i = 0; i < faceData.count; i++) {
FaceModel *fm = faceData[i];
FaceButton *btn = self.buttons[i];
btn.hidden = NO;
// [btn setImage:[UIImage imageNamed:fm.faceIcon] forState:UIControlStateNormal];
[self decodedImageWithImageName:fm.faceIcon block:^(UIImage *image) {
[btn setImage:image forState:UIControlStateNormal];
}];
btn.faceTitle = fm.faceTitle;
}
网友评论