SDWebImage中,我们可以使用URL给ImageView赋值,实现异步赋值,保持正确并且不卡顿,现在有这样一个需求,图片是张二维码,我们需要耗时计算出二维码的图片,添加到tableView中,为了保证正确,因此二维码的生成是同步的,因此造成了tableView的卡顿,以及页面跳转的延迟。
- 为了优化该问题,这里参考并使用了SDWebImage的一些Category,做了一个中间件,用来实现异步给图片赋值,但不使用URL, 图片的生成方式就很多种多样了,可以充分发挥想想,比如cpu,gpu算出来,比如接口上获取的base64转出来,等等。
UIView+WebCacheOperation.h
这里借用了,SDWebImageView的UIView+WebCacheOperation.h
, 这个分类主要是定义了异步设置图片的操作”Operation“的缓存、取消
UIImageView+AsyncImage.h
然后,新建异步设置图片的新类以代替UIImageView+WebCache.h,仅仅是异步赋值的壳子,没有涉及图片的缓存!!
代码很简单,我这就直接贴了
//
// UIImageView+AsyncImage.h
// mmspc
//
// Created by admin on 2021/2/5.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef UIImage *_Nullable(^InputBlock)(void);
typedef InputBlock _Nullable(^InputExecuteBlock)(void);
typedef void(^OutputBlock)(UIImage *);
/// 异步设置图片,使用了SDWebImage的分类,
@interface UIImageView (AsyncImage)
/// 异步设置图片,进负责设置图片,不负责图片的缓存
/// @param asyncBlock 设置图片的方法,通过传回一个返回imageView的block实现图片的设置
/// @param placeholder 默认图
/// @param complete 完成事件
- (void)ky_asyncSetImageWithBlock:(nullable InputExecuteBlock)asyncBlock
placeholderImage:(nullable UIImage *)placeholder
completeBlock:(OutputBlock)complete;
@end
NS_ASSUME_NONNULL_END
//
// UIImageView+AsyncImage.m
// mmspc
//
// Created by admin on 2021/2/5.
//
#import "UIImageView+AsyncImage.h"
#import "UIView+WebCacheOperation.h"
@interface KYWebImageCombinedOperation : NSObject <SDWebImageOperation>
@property (assign, nonatomic, getter = isCancelled) BOOL cancelled;
@property (copy, nonatomic, nullable) SDWebImageNoParamsBlock cancelBlock;
@property (strong, nonatomic, nullable) NSOperation *transformOperation;
@end
@implementation UIImageView (AsyncImage)
- (void)ky_asyncSetImageWithBlock:(nullable InputExecuteBlock)asyncBlock
placeholderImage:(nullable UIImage *)placeholder
completeBlock:(OutputBlock)complete {
NSString *validOperationKey = NSStringFromClass([self class]);
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
dispatch_main_async_safe(^{
[self ky_setImage:placeholder];
});
InputBlock block = asyncBlock();
__weak __typeof(self)wself = self;
NSOperation *operation = [NSOperation new];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if (operation.isCancelled) {
// do not call the completion if cancelled
return;
}
@autoreleasepool {
UIImage * qrImage = block();//[SGQRCodeGenerateManager generateWithLogoQRCodeData:@"扫描上方的二维码,立即申请 %@ 贷款!" logoImageName:@"wechat" logoScaleToSuperView:0.3];
NSLog(@"%@", NSThread.currentThread);
__strong __typeof (wself) sself = wself;
// [sself ky_removeActivityIndicator];
if (!sself) {
return;
}
if (complete) {
complete(qrImage);
}
dispatch_main_async_safe(^{
if (!sself) {
return;
}
if (qrImage) {
[sself ky_setImage:qrImage];
[sself ky_setNeedsLayout];
} else {
[sself ky_setImage:placeholder];
[sself ky_setNeedsLayout];
}
});
}
});
[self sd_setImageLoadOperation:operation forKey:validOperationKey];
}
- (void)ky_setImage:(UIImage *)image {
if ([self isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)self;
imageView.image = image;
}
}
- (void)ky_setNeedsLayout {
[self setNeedsLayout];
}
@end
// 模仿WebCache的Opreation,创建自己的KYWebImageCombinedOperation
@implementation KYWebImageCombinedOperation
- (void)setCancelBlock:(nullable SDWebImageNoParamsBlock)cancelBlock {
// check if the operation is already cancelled, then we just call the cancelBlock
if (self.isCancelled) {
if (cancelBlock) {
cancelBlock();
}
_cancelBlock = nil; // don't forget to nil the cancelBlock, otherwise we will get crashes
} else {
_cancelBlock = [cancelBlock copy];
}
}
- (void)cancel {
self.cancelled = YES;
// if (self.cacheOperation) {
// [self.cacheOperation cancel];
// self.cacheOperation = nil;
// }
if (self.cancelBlock) {
self.cancelBlock();
// TODO: this is a temporary fix to #809.
// Until we can figure the exact cause of the crash, going with the ivar instead of the setter
// self.cancelBlock = nil;
_cancelBlock = nil;
}
}
@end
使用方式
[self.codeImageView ky_asyncSetImageWithBlock:^InputBlock _Nullable {
InputBlock block = ^UIImage * _Nullable {
UIImage * image = model.qrCodeImage;
if (!image) {
NSString * logoName = model.codeScanBy == 0 ? @"wechat" : @"logo";
// 这里就是一个同步计算图片的方法,如果在主线程的话,就会引起卡顿了
image = [SGQRCodeGenerateManager generateWithLogoQRCodeData:@"1" logoImageName:logoName logoScaleToSuperView:0.3];
}
return image;
};
return block;
} placeholderImage:[UIImage imageNamed:@"logo"] completeBlock:^(UIImage * _Nonnull image) {
model.qrCodeImage = image;
}];
网友评论