Skip to content

Commit

Permalink
Updated SDWebImage to v4.2.1
Browse files Browse the repository at this point in the history
Updated SDWebImage to v4.2.1
  • Loading branch information
ko1o committed Nov 3, 2017
1 parent 8d2a4ed commit 14707c9
Show file tree
Hide file tree
Showing 42 changed files with 2,404 additions and 972 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#if __has_include(<FLAnimatedImage/FLAnimatedImage.h>)
#import <FLAnimatedImage/FLAnimatedImage.h>
#else
#import "FLAnimatedImageView.h"
#import "FLAnimatedImage.h"
#endif

#import "SDWebImageManager.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#import "UIView+WebCacheOperation.h"
#import "UIView+WebCache.h"
#import "NSData+ImageContentType.h"
#import "FLAnimatedImage.h"
#import "UIImageView+WebCache.h"

@implementation FLAnimatedImageView (WebCache)
Expand Down Expand Up @@ -53,17 +52,25 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url
options:options
operationKey:nil
setImageBlock:^(UIImage *image, NSData *imageData) {
// This setImageBlock may not called from main queue
SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:imageData];
FLAnimatedImage *animatedImage;
if (imageFormat == SDImageFormatGIF) {
weakSelf.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
weakSelf.image = nil;
} else {
weakSelf.image = image;
weakSelf.animatedImage = nil;
animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
}
dispatch_main_async_safe(^{
if (animatedImage) {
weakSelf.animatedImage = animatedImage;
weakSelf.image = nil;
} else {
weakSelf.image = image;
weakSelf.animatedImage = nil;
}
});
}
progress:progressBlock
completed:completedBlock];
completed:completedBlock
context:@{SDWebImageInternalSetImageInGlobalQueueKey: @(YES)}];
}

@end
Expand Down
11 changes: 10 additions & 1 deletion Dependency/SDWebImage/NSData+ImageContentType.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ typedef NS_ENUM(NSInteger, SDImageFormat) {
SDImageFormatPNG,
SDImageFormatGIF,
SDImageFormatTIFF,
SDImageFormatWebP
SDImageFormatWebP,
SDImageFormatHEIC
};

@interface NSData (ImageContentType)
Expand All @@ -30,4 +31,12 @@ typedef NS_ENUM(NSInteger, SDImageFormat) {
*/
+ (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data;

/**
Convert SDImageFormat to UTType
@param format Format as SDImageFormat
@return The UTType as CFStringRef
*/
+ (nonnull CFStringRef)sd_UTTypeFromSDImageFormat:(SDImageFormat)format;

@end
68 changes: 60 additions & 8 deletions Dependency/SDWebImage/NSData+ImageContentType.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
*/

#import "NSData+ImageContentType.h"
#if SD_MAC
#import <CoreServices/CoreServices.h>
#else
#import <MobileCoreServices/MobileCoreServices.h>
#endif

// Currently Image/IO does not support WebP
#define kSDUTTypeWebP ((__bridge CFStringRef)@"public.webp")
// AVFileTypeHEIC is defined in AVFoundation via iOS 11, we use this without import AVFoundation
#define kSDUTTypeHEIC ((__bridge CFStringRef)@"public.heic")

@implementation NSData (ImageContentType)

Expand All @@ -17,6 +26,7 @@ + (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data {
return SDImageFormatUndefined;
}

// File signatures table: http://www.garykessler.net/library/file_sigs.html
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
Expand All @@ -29,18 +39,60 @@ + (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data {
case 0x49:
case 0x4D:
return SDImageFormatTIFF;
case 0x52:
// R as RIFF for WEBP
if (data.length < 12) {
return SDImageFormatUndefined;
case 0x52: {
if (data.length >= 12) {
//RIFF....WEBP
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return SDImageFormatWebP;
}
}

NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return SDImageFormatWebP;
break;
}
case 0x00: {
if (data.length >= 12) {
//....ftypheic ....ftypheix ....ftyphevc ....ftyphevx
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(4, 8)] encoding:NSASCIIStringEncoding];
if ([testString isEqualToString:@"ftypheic"]
|| [testString isEqualToString:@"ftypheix"]
|| [testString isEqualToString:@"ftyphevc"]
|| [testString isEqualToString:@"ftyphevx"]) {
return SDImageFormatHEIC;
}
}
break;
}
}
return SDImageFormatUndefined;
}

+ (nonnull CFStringRef)sd_UTTypeFromSDImageFormat:(SDImageFormat)format {
CFStringRef UTType;
switch (format) {
case SDImageFormatJPEG:
UTType = kUTTypeJPEG;
break;
case SDImageFormatPNG:
UTType = kUTTypePNG;
break;
case SDImageFormatGIF:
UTType = kUTTypeGIF;
break;
case SDImageFormatTIFF:
UTType = kUTTypeTIFF;
break;
case SDImageFormatWebP:
UTType = kSDUTTypeWebP;
break;
case SDImageFormatHEIC:
UTType = kSDUTTypeHEIC;
break;
default:
// default is kUTTypePNG
UTType = kUTTypePNG;
break;
}
return UTType;
}

@end
11 changes: 10 additions & 1 deletion Dependency/SDWebImage/NSImage+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ - (CGImageRef)CGImage {
}

- (BOOL)isGIF {
return NO;
BOOL isGIF = NO;
for (NSImageRep *rep in self.representations) {
if ([rep isKindOfClass:[NSBitmapImageRep class]]) {
NSBitmapImageRep *bitmapRep = (NSBitmapImageRep *)rep;
NSUInteger frameCount = [[bitmapRep valueForProperty:NSImageFrameCount] unsignedIntegerValue];
isGIF = frameCount > 1 ? YES : NO;
break;
}
}
return isGIF;
}

@end
Expand Down
30 changes: 14 additions & 16 deletions Dependency/SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
*/

#import "SDImageCache.h"
#import "SDWebImageDecoder.h"
#import "UIImage+MultiFormat.h"
#import <CommonCrypto/CommonDigest.h>
#import "UIImage+GIF.h"
#import "NSData+ImageContentType.h"
#import "NSImage+WebCache.h"
#import "SDWebImageCodersManager.h"

// See https://github.com/rs/SDWebImage/pull/1141 for discussion
@interface AutoPurgeCache : NSCache
Expand Down Expand Up @@ -171,10 +168,11 @@ - (nullable NSString *)cachedFileNameForKey:(nullable NSString *)key {
}
unsigned char r[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (CC_LONG)strlen(str), r);
NSURL *keyURL = [NSURL URLWithString:key];
NSString *ext = keyURL ? keyURL.pathExtension : key.pathExtension;
NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%@",
r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10],
r[11], r[12], r[13], r[14], r[15], [key.pathExtension isEqualToString:@""] ? @"" : [NSString stringWithFormat:@".%@", key.pathExtension]];

r[11], r[12], r[13], r[14], r[15], ext.length == 0 ? @"" : [NSString stringWithFormat:@".%@", ext]];
return filename;
}

Expand Down Expand Up @@ -220,9 +218,9 @@ - (void)storeImage:(nullable UIImage *)image
@autoreleasepool {
NSData *data = imageData;
if (!data && image) {
SDImageFormat imageFormatFromData = [NSData sd_imageFormatForImageData:data];
data = [image sd_imageDataAsFormat:imageFormatFromData];
}
// If we do not have any data to detect image format, use PNG format
data = [[SDWebImageCodersManager sharedInstance] encodedDataWithImage:image format:SDImageFormatPNG];
}
[self storeImageDataToDisk:data forKey:key];
}

Expand Down Expand Up @@ -311,29 +309,29 @@ - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key {

- (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *)key {
NSString *defaultPath = [self defaultCachePathForKey:key];
NSData *data = [NSData dataWithContentsOfFile:defaultPath];
NSData *data = [NSData dataWithContentsOfFile:defaultPath options:self.config.diskCacheReadingOptions error:nil];
if (data) {
return data;
}

// fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
// checking the key with and without the extension
data = [NSData dataWithContentsOfFile:defaultPath.stringByDeletingPathExtension];
data = [NSData dataWithContentsOfFile:defaultPath.stringByDeletingPathExtension options:self.config.diskCacheReadingOptions error:nil];
if (data) {
return data;
}

NSArray<NSString *> *customPaths = [self.customPaths copy];
for (NSString *path in customPaths) {
NSString *filePath = [self cachePathForKey:key inPath:path];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
NSData *imageData = [NSData dataWithContentsOfFile:filePath options:self.config.diskCacheReadingOptions error:nil];
if (imageData) {
return imageData;
}

// fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
// checking the key with and without the extension
imageData = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension];
imageData = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension options:self.config.diskCacheReadingOptions error:nil];
if (imageData) {
return imageData;
}
Expand All @@ -345,10 +343,10 @@ - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key {
NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
if (data) {
UIImage *image = [UIImage sd_imageWithData:data];
UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data];
image = [self scaledImageForKey:key image:image];
if (self.config.shouldDecompressImages) {
image = [UIImage decodedImageWithImage:image];
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}];
}
return image;
} else {
Expand All @@ -372,7 +370,7 @@ - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key don
UIImage *image = [self imageFromMemoryCacheForKey:key];
if (image) {
NSData *diskData = nil;
if ([image isGIF]) {
if (image.images) {
diskData = [self diskImageDataBySearchingAllPathsForKey:key];
}
if (doneBlock) {
Expand Down
10 changes: 8 additions & 2 deletions Dependency/SDWebImage/SDImageCacheConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@property (assign, nonatomic) BOOL shouldDecompressImages;

/**
* disable iCloud backup [defaults to YES]
* disable iCloud backup [defaults to YES]
*/
@property (assign, nonatomic) BOOL shouldDisableiCloud;

Expand All @@ -28,7 +28,13 @@
@property (assign, nonatomic) BOOL shouldCacheImagesInMemory;

/**
* The maximum length of time to keep an image in the cache, in seconds
* The reading options while reading cache from disk.
* Defaults to 0. You can set this to mapped file to improve performance.
*/
@property (assign, nonatomic) NSDataReadingOptions diskCacheReadingOptions;

/**
* The maximum length of time to keep an image in the cache, in seconds.
*/
@property (assign, nonatomic) NSInteger maxCacheAge;

Expand Down
1 change: 1 addition & 0 deletions Dependency/SDWebImage/SDImageCacheConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ - (instancetype)init {
_shouldDecompressImages = YES;
_shouldDisableiCloud = YES;
_shouldCacheImagesInMemory = YES;
_diskCacheReadingOptions = 0;
_maxCacheAge = kDefaultCacheMaxCacheAge;
_maxCacheSize = 0;
}
Expand Down
Loading

0 comments on commit 14707c9

Please sign in to comment.