Skip to content

Commit

Permalink
Updated SDWebImage and removed mapkit dependancy.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaterfall committed Jun 9, 2012
1 parent eab91b8 commit 76a91ed
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 199 deletions.
93 changes: 0 additions & 93 deletions MWPhotoBrowser/Libraries/SDWebImage/MKAnnotationView+WebCache.h

This file was deleted.

80 changes: 0 additions & 80 deletions MWPhotoBrowser/Libraries/SDWebImage/MKAnnotationView+WebCache.m

This file was deleted.

4 changes: 2 additions & 2 deletions MWPhotoBrowser/Libraries/SDWebImage/README.md
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
43 changes: 41 additions & 2 deletions MWPhotoBrowser/Libraries/SDWebImage/SDImageCache.m
Expand Up @@ -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

Expand Down Expand Up @@ -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:)])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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];
}
}
Expand Down
1 change: 1 addition & 0 deletions MWPhotoBrowser/Libraries/SDWebImage/SDWebImageManager.h
Expand Up @@ -39,6 +39,7 @@ typedef enum
*/
@interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate>
{
NSMutableArray *downloadInfo;
NSMutableArray *downloadDelegates;
NSMutableArray *downloaders;
NSMutableArray *cacheDelegates;
Expand Down
27 changes: 16 additions & 11 deletions MWPhotoBrowser/Libraries/SDWebImage/SDWebImageManager.m
Expand Up @@ -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];
Expand All @@ -40,6 +41,7 @@ - (id)init

- (void)dealloc
{
SDWISafeRelease(downloadInfo);
SDWISafeRelease(downloadDelegates);
SDWISafeRelease(downloaders);
SDWISafeRelease(cacheDelegates);
Expand Down Expand Up @@ -193,6 +195,7 @@ - (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate
{
SDWebImageDownloader *downloader = SDWIReturnRetained([downloaders objectAtIndex:idx]);

[downloadInfo removeObjectAtIndex:idx];
[downloadDelegates removeObjectAtIndex:idx];
[downloaders removeObjectAtIndex:idx];

Expand Down Expand Up @@ -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);
}

Expand All @@ -301,6 +303,7 @@ - (void)imageCache:(SDImageCache *)imageCache didNotFindImageForKey:(NSString *)
downloader.progressive = YES;
}

[downloadInfo addObject:info];
[downloadDelegates addObject:delegate];
[downloaders addObject:downloader];
}
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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];
}
}
Expand Down Expand Up @@ -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];
}
}
Expand Down

0 comments on commit 76a91ed

Please sign in to comment.