多个imageNamed:方法导致UI加载卡顿解决方案:

2019-01-24  本文已影响5人  岁月如歌的季节

使用场景:

当表情键盘使用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;
    }
上一篇下一篇

猜你喜欢

热点阅读