Permalink
Browse files

Updated SDWebImage and removed mapkit dependancy.

  • Loading branch information...
1 parent eab91b8 commit 76a91ed5b74e1d8897867f66de11381d9d1d4ae5 @mwaterfall committed Jun 9, 2012
@@ -1,93 +0,0 @@
-//
-// MKAnnotationView+WebCache.h
-// SDWebImage
-//
-// Created by Olivier Poitrey on 14/03/12.
-// Copyright (c) 2012 Dailymotion. All rights reserved.
-//
-
-#import "MapKit/MapKit.h"
-#import "SDWebImageCompat.h"
-#import "SDWebImageManagerDelegate.h"
-#import "SDWebImageManager.h"
-
-/**
- * Integrates SDWebImage async downloading and caching of remote images with MKAnnotationView.
- */
-@interface MKAnnotationView (WebCache) <SDWebImageManagerDelegate>
-
-/**
- * Set the imageView `image` with an `url`.
- *
- * The downloand is asynchronous and cached.
- *
- * @param url The url for the image.
- */
-- (void)setImageWithURL:(NSURL *)url;
-
-/**
- * Set the imageView `image` with an `url` and a placeholder.
- *
- * The downloand is asynchronous and cached.
- *
- * @param url The url for the image.
- * @param placeholder The image to be set initially, until the image request finishes.
- * @see setImageWithURL:placeholderImage:options:
- */
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
-
-/**
- * Set the imageView `image` with an `url`, placeholder and custom options.
- *
- * The downloand is asynchronous and cached.
- *
- * @param url The url for the image.
- * @param placeholder The image to be set initially, until the image request finishes.
- * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
- */
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
-
-#if NS_BLOCKS_AVAILABLE
-/**
- * Set the imageView `image` with an `url`.
- *
- * The downloand is asynchronous and cached.
- *
- * @param url The url for the image.
- * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument.
- * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil).
- */
-- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
-
-/**
- * Set the imageView `image` with an `url`, placeholder.
- *
- * The downloand is asynchronous and cached.
- *
- * @param url The url for the image.
- * @param placeholder The image to be set initially, until the image request finishes.
- * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument.
- * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil).
- */
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
-
-/**
- * Set the imageView `image` with an `url`, placeholder and custom options.
- *
- * The downloand is asynchronous and cached.
- *
- * @param url The url for the image.
- * @param placeholder The image to be set initially, until the image request finishes.
- * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
- * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument.
- * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil).
- */
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
-#endif
-
-/**
- * Cancel the current download
- */
-- (void)cancelCurrentImageLoad;
-
-@end
@@ -1,80 +0,0 @@
-//
-// MKAnnotationView+WebCache.m
-// SDWebImage
-//
-// Created by Olivier Poitrey on 14/03/12.
-// Copyright (c) 2012 Dailymotion. All rights reserved.
-//
-
-#import "MKAnnotationView+WebCache.h"
-
-@implementation MKAnnotationView (WebCache)
-
-- (void)setImageWithURL:(NSURL *)url
-{
- [self setImageWithURL:url placeholderImage:nil];
-}
-
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
-{
- [self setImageWithURL:url placeholderImage:placeholder options:0];
-}
-
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
-{
- SDWebImageManager *manager = [SDWebImageManager sharedManager];
-
- // Remove in progress downloader from queue
- [manager cancelForDelegate:self];
-
- self.image = placeholder;
-
- if (url)
- {
- [manager downloadWithURL:url delegate:self options:options];
- }
-}
-
-#if NS_BLOCKS_AVAILABLE
-- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
-{
- [self setImageWithURL:url placeholderImage:nil success:success failure:failure];
-}
-
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
-{
- [self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure];
-}
-
-- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
-{
- SDWebImageManager *manager = [SDWebImageManager sharedManager];
-
- // Remove in progress downloader from queue
- [manager cancelForDelegate:self];
-
- self.image = placeholder;
-
- if (url)
- {
- [manager downloadWithURL:url delegate:self options:options success:success failure:failure];
- }
-}
-#endif
-
-- (void)cancelCurrentImageLoad
-{
- [[SDWebImageManager sharedManager] cancelForDelegate:self];
-}
-
-- (void)webImageManager:(SDWebImageManager *)imageManager didProgressWithPartialImage:(UIImage *)image forURL:(NSURL *)url
-{
- self.image = image;
-}
-
-- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
-{
- self.image = image;
-}
-
-@end
@@ -244,7 +244,7 @@ In you application project app’s target settings, find the "Build Phases" sect
![Add Target Dependencies](http://dl.dropbox.com/u/123346/SDWebImage/04_add_target_dependencies.jpg)
-Click the "+" button and select "SDWebImage ARC" (you may choose the non ARC target if you want to support iOS <3):
+Click the "+" button and select "SDWebImage ARC" (you may choose the non ARC target if you want to support iOS <3 or the ARC+MKAnnotation if you need MapKit category):
![Add Target Dependencies Dialog](http://dl.dropbox.com/u/123346/SDWebImage/05_add_target_dependencies_dialog.jpg)
@@ -256,7 +256,7 @@ Click the "+" button and select "libSDWebImageARC.a" library (use non ARC versio
![Add Library Link Dialog](http://dl.dropbox.com/u/123346/SDWebImage/07_add_library_link_dialog.jpg)
-Click the "+" button again and select "MapKit.framework", this is used by MKAnnotationView+WebCache category:
+If you chose to link against the ARC+MKAnnotation target, click the "+" button again and select "MapKit.framework":
![Add ImageIO Framework](http://dl.dropbox.com/u/123346/SDWebImage/08_add_imageio_framework.jpg)
@@ -10,10 +10,37 @@
#import "SDWebImageDecoder.h"
#import <CommonCrypto/CommonDigest.h>
#import "SDWebImageDecoder.h"
+#import <mach/mach.h>
+#import <mach/mach_host.h>
+
+static SDImageCache *instance;
static NSInteger cacheMaxCacheAge = 60*60*24*7; // 1 week
+static natural_t minFreeMemLeft = 1024*1024*12; // reserve 12MB RAM
-static SDImageCache *instance;
+// inspired by http://stackoverflow.com/questions/5012886/knowing-available-ram-on-an-ios-device
+static natural_t get_free_memory(void)
+{
+ mach_port_t host_port;
+ mach_msg_type_number_t host_size;
+ vm_size_t pagesize;
+
+ host_port = mach_host_self();
+ host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
+ host_page_size(host_port, &pagesize);
+
+ vm_statistics_data_t vm_stat;
+
+ if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
+ {
+ NSLog(@"Failed to fetch vm statistics");
+ return 0;
+ }
+
+ /* Stats in bytes */
+ natural_t mem_free = vm_stat.free_count * pagesize;
+ return mem_free;
+}
@implementation SDImageCache
@@ -151,6 +178,10 @@ - (void)notifyDelegate:(NSDictionary *)arguments
if (image)
{
+ if (get_free_memory() < minFreeMemLeft)
+ {
+ [memCache removeAllObjects];
+ }
[memCache setObject:image forKey:key];
if ([delegate respondsToSelector:@selector(imageCache:didFindImage:forKey:userInfo:)])
@@ -196,7 +227,11 @@ - (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)
{
return;
}
-
+
+ if (get_free_memory() < minFreeMemLeft)
+ {
+ [memCache removeAllObjects];
+ }
[memCache setObject:image forKey:key];
if (toDisk)
@@ -248,6 +283,10 @@ - (UIImage *)imageFromKey:(NSString *)key fromDisk:(BOOL)fromDisk
image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
if (image)
{
+ if (get_free_memory() < minFreeMemLeft)
+ {
+ [memCache removeAllObjects];
+ }
[memCache setObject:image forKey:key];
}
}
@@ -39,6 +39,7 @@ typedef enum
*/
@interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate>
{
+ NSMutableArray *downloadInfo;
NSMutableArray *downloadDelegates;
NSMutableArray *downloaders;
NSMutableArray *cacheDelegates;
@@ -28,6 +28,7 @@ - (id)init
{
if ((self = [super init]))
{
+ downloadInfo = [[NSMutableArray alloc] init];
downloadDelegates = [[NSMutableArray alloc] init];
downloaders = [[NSMutableArray alloc] init];
cacheDelegates = [[NSMutableArray alloc] init];
@@ -40,6 +41,7 @@ - (id)init
- (void)dealloc
{
+ SDWISafeRelease(downloadInfo);
SDWISafeRelease(downloadDelegates);
SDWISafeRelease(downloaders);
SDWISafeRelease(cacheDelegates);
@@ -193,6 +195,7 @@ - (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate
{
SDWebImageDownloader *downloader = SDWIReturnRetained([downloaders objectAtIndex:idx]);
+ [downloadInfo removeObjectAtIndex:idx];
[downloadDelegates removeObjectAtIndex:idx];
[downloaders removeObjectAtIndex:idx];
@@ -291,7 +294,6 @@ - (void)imageCache:(SDImageCache *)imageCache didNotFindImageForKey:(NSString *)
else
{
// Reuse shared downloader
- downloader.userInfo = info; // TOFIX: here we overload previous userInfo
downloader.lowPriority = (options & SDWebImageLowPriority);
}
@@ -301,6 +303,7 @@ - (void)imageCache:(SDImageCache *)imageCache didNotFindImageForKey:(NSString *)
downloader.progressive = YES;
}
+ [downloadInfo addObject:info];
[downloadDelegates addObject:delegate];
[downloaders addObject:downloader];
}
@@ -326,7 +329,7 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didUpdatePartialImage
}
if ([delegate respondsToSelector:@selector(webImageManager:didProgressWithPartialImage:forURL:userInfo:)])
{
- NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
+ NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
@@ -365,17 +368,17 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
}
if ([delegate respondsToSelector:@selector(webImageManager:didFinishWithImage:forURL:userInfo:)])
{
- NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
+ NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:userInfo:), self, image, downloader.url, userInfo);
}
#if NS_BLOCKS_AVAILABLE
- if ([downloader.userInfo objectForKey:@"success"])
+ if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"success"])
{
- SuccessBlock success = [downloader.userInfo objectForKey:@"success"];
+ SuccessBlock success = [[downloadInfo objectAtIndex:uidx] objectForKey:@"success"];
success(image);
}
#endif
@@ -392,23 +395,24 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
}
if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)])
{
- NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
+ NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, nil, downloader.url, userInfo);
}
#if NS_BLOCKS_AVAILABLE
- if ([downloader.userInfo objectForKey:@"failure"])
+ if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"])
{
- FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
+ FailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"];
failure(nil);
}
#endif
}
[downloaders removeObjectAtIndex:uidx];
+ [downloadInfo removeObjectAtIndex:uidx];
[downloadDelegates removeObjectAtIndex:uidx];
}
}
@@ -459,22 +463,23 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFailWithError:(NSE
}
if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)])
{
- NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
+ NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, error, downloader.url, userInfo);
}
#if NS_BLOCKS_AVAILABLE
- if ([downloader.userInfo objectForKey:@"failure"])
+ if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"])
{
- FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
+ FailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"];
failure(error);
}
#endif
[downloaders removeObjectAtIndex:uidx];
+ [downloadInfo removeObjectAtIndex:uidx];
[downloadDelegates removeObjectAtIndex:uidx];
}
}
Oops, something went wrong.

0 comments on commit 76a91ed

Please sign in to comment.