QQ泡沫乐园 · 免费提供游戏辅助,破解软件,活动资讯,喜欢记得收藏哦!
综合软件_线报活动_游戏辅助_最新电影_最优质的的辅助分享平台

使用SDWebImage加载图片的方法有哪些?(二)——SDWebImage

网络 2023-01-08 21:00

近期在项目开发中发觉使用SDWebImage加载图片不显示,所以去查了一波资料,大致整理了下。

SDWebImage 本身是有支持https的,所以我那边替换了加载图片的方式。具体缘由应当是因为项目中使用了SSL证书引起的。

简单更改一个图片加载的问题:

-(void)sd_setImageWithURL:(NSURL )url placeholderImage:(UIImage )placeholder options:(SDWebImageOptions)options;
进入系统可以查看到options里面的选项
options:
//失败后重试
SDWebImageRetryFailed = 1 << 0,
//UI交互期间开始下载,导致延迟下载比如UIScrollView减速。
SDWebImageLowPriority = 1 << 1,
//只进行内存缓存

微博加载失败_微博桌面 加载图片失败_微博老是加载图片失败

SDWebImageCacheMemoryOnly = 1 //SDWebImageAllowInvalidSSLCertificates = 1 << 7, //优先下载 SDWebImageHighPriority = 1 << 8, //延迟占位符 SDWebImageDelayPlaceholder = 1 << 9, //改变动画形象 SDWebImageTransformAnimatedImage = 1 << 10,

我们选用SSL证书即可解决;

当时因为考虑到其他项目也可能会遇见,但是假如更改上去不是很方便,所以我那边去查看了下其他解决方案就是我们去更改SDWebImage内部的函数;

我们找到 UIImageView+WebCache.m 这个文件,内部去更改

- (void)sd_setImageWithURL:(NSURL *)url {
    [self sd_setImageWithURL:url placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates progress:nil completed:nil];
}
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
    [self sd_setImageWithURL:url placeholderImage:placeholder options:SDWebImageAllowInvalidSSLCertificates progress:nil completed:nil];
}

这样更改可以达到项目中只须要更改第三方库其他地方都不用更改。这样对于一个项目中多处使用SDWebImage的时侯回便捷好多。但是这些技巧不建议团队使用,更不建议使用了cocoPods的项目使用。

当时在网上见到一个黑魔法是写一个image的分类,这样的话就不用去更改之前我们加载的图片了:

UIImageView+YJHttps.h
#import 
@interface UIImageView (YJHttps)
@end

UIImageView+YJHttps.m
#import "UIImageView+YJHttps.h"
#import 
#import 
@implementation UIImageView (YJHttps)
+ (void)load {
    Class myClass = [self class];
    
    // 获取SEL
    SEL originSetImageSel = @selector(sd_setImageWithURL:placeholderImage:options:progress:completed:);
    SEL newSetImageSel = @selector(sd_setHttpsImageWithURL:placeholderImage:options:progress:completed:);
    
    // 生成Method

微博桌面 加载图片失败_微博老是加载图片失败_微博加载失败

Method originMethod = class_getInstanceMethod(myClass, originSetImageSel); Method newMethod = class_getInstanceMethod(myClass, newSetImageSel); // 交换方法实现 method_exchangeImplementations(originMethod, newMethod); } - (void)sd_setHttpsImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock { NSLog(@"这里实现了"); [self sd_setHttpsImageWithURL:url placeholderImage:placeholder options:SDWebImageAllowInvalidSSLCertificates progress:progressBlock completed:completedBlock]; } @end

但是在分享会上朋友说:方法替换黑魔法在项目上面不建议使用太多,它会捣乱方式现有的调用次序,如果放在某个泛型上面,就要谨慎了,出bug的话调试上去会很绕的。

所以分享会上朋友也提出了更好的一个办法就是写一个分类,里面将加载图片你的函数换个名子,然后全局替换项目中使用的地方,这样我们的改动也会很小,但是起到的疗效应当是挺好的。

以上4中方式仅供参考。如果有好的方式也可留言出来,我会再度整理。