如果圖片命名沒有指定@2x或者@3x的話碉纺,3.7.3版本SDWebImage會默認(rèn)按照屏幕的scale來創(chuàng)建圖片森爽。
inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image) {
if (!image) {
return nil;
}
if ([image.images count] > 0) {
NSMutableArray *scaledImages = [NSMutableArray array];
for (UIImage *tempImage in image.images) {
[scaledImages addObject:SDScaledImageForKey(key, tempImage)];
}
return [UIImage animatedImageWithImages:scaledImages duration:image.duration];
}
else {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
CGFloat scale = [UIScreen mainScreen].scale;
if (key.length >= 8) {
NSRange range = [key rangeOfString:@"@2x."];
if (range.location != NSNotFound) {
scale = 2.0;
}
range = [key rangeOfString:@"@3x."];
if (range.location != NSNotFound) {
scale = 3.0;
}
}
UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
image = scaledImage;
}
return image;
}
}
** CGFloat scale = [UIScreen mainScreen].scale;
**
到Github的issue里搜索 SDScaledImageForKey
有很多人對這個設(shè)計也表示了懷疑,作者有一些回應(yīng)朋截,有興趣的同行可以去看看嚎朽。
而到了4.0之后,不再按照這個規(guī)則了卿城,而是默認(rèn)是1枚钓。但是從代碼看,好像臨時改成1的樣子:
#elif SD_UIKIT
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
#endif
CGFloat scale = 1;
它還保留著對屏幕scale是否可用的if判斷藻雪。
我想4.0的處理應(yīng)該是合理的秘噪。
想一下,一個100x100像素的圖像勉耀,如果設(shè)計師設(shè)計的時候期望它是一個2倍圖指煎,也就是說應(yīng)該占用屏幕50x50個點(diǎn),但是便斥,3.7的處理使得在Plus機(jī)型上就只有33X33個點(diǎn)了至壤。
BTW: 順便劈了一眼SDWebImage的代碼。
圖像從磁盤緩存回來之后枢纠,需要做三次轉(zhuǎn)換才到最終的圖片像街,這性能真的沒問題的嗎?好擔(dān)心晋渺。镰绎。。木西。
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key {
NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
if (data) {
UIImage *image = [UIImage sd_imageWithData:data];
image = [self scaledImageForKey:key image:image];
if (self.config.shouldDecompressImages) {
image = [UIImage decodedImageWithImage:image];
}
return image;
}
else {
return nil;
}
}