Fix and make SDScaledImageForPath extensible
This commit is contained in:
parent
ae1476b3f2
commit
55ebe66b4c
|
@ -164,8 +164,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second check the disk cache...
|
// Second check the disk cache...
|
||||||
UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])];
|
UIImage *diskImage = [self diskImageForKey:key];
|
||||||
|
|
||||||
if (diskImage)
|
if (diskImage)
|
||||||
{
|
{
|
||||||
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
|
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;
|
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
|
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock
|
||||||
{
|
{
|
||||||
if (!doneBlock) return;
|
if (!doneBlock) return;
|
||||||
|
@ -197,8 +217,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
|
||||||
{
|
{
|
||||||
@autoreleasepool
|
@autoreleasepool
|
||||||
{
|
{
|
||||||
UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])];
|
UIImage *diskImage = [self diskImageForKey:key];
|
||||||
|
|
||||||
if (diskImage)
|
if (diskImage)
|
||||||
{
|
{
|
||||||
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
|
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
|
||||||
|
|
|
@ -37,4 +37,4 @@
|
||||||
#define SDDispatchQueueSetterSementics assign
|
#define SDDispatchQueueSetterSementics assign
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData);
|
extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image);
|
|
@ -12,34 +12,15 @@
|
||||||
#error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
|
#error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
|
||||||
#endif
|
#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)])
|
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
|
||||||
{
|
{
|
||||||
CGFloat scale = 1.0;
|
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)
|
// 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)
|
if (range.location != NSNotFound)
|
||||||
{
|
{
|
||||||
scale = 2.0;
|
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];
|
UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
|
||||||
image = scaledImage;
|
image = scaledImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
|
@ -225,7 +225,9 @@
|
||||||
|
|
||||||
if (partialImageRef)
|
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);
|
CGImageRelease(partialImageRef);
|
||||||
dispatch_async(dispatch_get_main_queue(), ^
|
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
|
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
|
||||||
{
|
{
|
||||||
CFRunLoopStop(CFRunLoopGetCurrent());
|
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||||
|
@ -267,7 +274,9 @@
|
||||||
}
|
}
|
||||||
else
|
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))
|
if (CGSizeEqualToSize(image.size, CGSizeZero))
|
||||||
{
|
{
|
||||||
completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES);
|
completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES);
|
||||||
|
|
Loading…
Reference in New Issue