Authored by Olivier Poitrey

Merge pull request #366 from bm-i/master

Fix and make SDScaledImageForPath extensible
... ... @@ -164,8 +164,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
}
// Second check the disk cache...
UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])];
UIImage *diskImage = [self diskImageForKey:key];
if (diskImage)
{
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
... ... @@ -175,6 +174,27 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
return diskImage;
}
- (UIImage *)diskImageForKey:(NSString *)key
{
NSString *path = [self cachePathForKey:key];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data)
{
UIImage *image = [[UIImage alloc] initWithData:data];
UIImage *scaledImage = [self scaledImageForKey:key image:image];
return [UIImage decodedImageWithImage:scaledImage];
}
else
{
return nil;
}
}
- (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image
{
return SDScaledImageForKey(key, image);
}
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock
{
if (!doneBlock) return;
... ... @@ -197,8 +217,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
{
@autoreleasepool
{
UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])];
UIImage *diskImage = [self diskImageForKey:key];
if (diskImage)
{
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
... ...
... ... @@ -37,4 +37,4 @@
#define SDDispatchQueueSetterSementics assign
#endif
extern inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData);
extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image);
\ No newline at end of file
... ...
... ... @@ -12,34 +12,15 @@
#error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
#endif
inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData)
inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image)
{
if (!imageOrData)
{
return nil;
}
UIImage *image = nil;
if ([imageOrData isKindOfClass:[NSData class]])
{
image = [[UIImage alloc] initWithData:(NSData *)imageOrData];
}
else if ([imageOrData isKindOfClass:[UIImage class]])
{
image = (UIImage *)imageOrData;
}
else
{
return nil;
}
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
CGFloat scale = 1.0;
if (path.length >= 8)
if (key.length >= 8)
{
// Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext)
NSRange range = [path rangeOfString:@"@2x." options:0 range:NSMakeRange(path.length - 8, 5)];
NSRange range = [key rangeOfString:@"@2x." options:0 range:NSMakeRange(key.length - 8, 5)];
if (range.location != NSNotFound)
{
scale = 2.0;
... ... @@ -49,6 +30,5 @@ inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData)
UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
image = scaledImage;
}
return image;
}
... ...
... ... @@ -225,7 +225,9 @@
if (partialImageRef)
{
UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, [UIImage imageWithCGImage:partialImageRef])];
UIImage *image = [UIImage imageWithCGImage:partialImageRef];
UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image];
image = [UIImage decodedImageWithImage:scaledImage];
CGImageRelease(partialImageRef);
dispatch_async(dispatch_get_main_queue(), ^
{
... ... @@ -248,6 +250,11 @@
}
}
- (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image
{
return SDScaledImageForKey(key, image);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
{
CFRunLoopStop(CFRunLoopGetCurrent());
... ... @@ -267,7 +274,9 @@
}
else
{
UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)];
UIImage *image = [[UIImage alloc] initWithData:self.imageData];
UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image];
image = [UIImage decodedImageWithImage:scaledImage];
if (CGSizeEqualToSize(image.size, CGSizeZero))
{
completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES);
... ...
... ... @@ -39,7 +39,7 @@
{
if ((self = [super init]))
{
_imageCache = [SDImageCache sharedImageCache];
_imageCache = [self createCache];
_imageDownloader = SDWebImageDownloader.new;
_failedURLs = NSMutableArray.new;
_runningOperations = NSMutableArray.new;
... ... @@ -47,6 +47,10 @@
return self;
}
- (SDImageCache *)createCache
{
return [SDImageCache sharedImageCache];
}
- (NSString *)cacheKeyForURL:(NSURL *)url
{
... ...