Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPV6 iOS 9 #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion JMImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "UIImageView+JMImageCache.h"
#import <CommonCrypto/CommonDigest.h>

@class JMImageCache;

Expand All @@ -18,7 +19,9 @@

@end

@interface JMImageCache : NSCache
@interface JMImageCache : NSCache{
NSURLSession *privateSession;
}

+ (JMImageCache *) sharedCache;

Expand All @@ -42,4 +45,6 @@
- (void) writeData:(NSData *)data toPath:(NSString *)path;
- (void) performDiskWriteOperation:(NSInvocation *)invoction;

+ (NSString *)SHA1FromString:(NSString *)string;

@end
51 changes: 25 additions & 26 deletions JMImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ + (JMImageCache *) sharedCache {
- (id) init {
self = [super init];
if(!self) return nil;

privateSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"JMImageCache"]];

self.diskOperationQueue = [[NSOperationQueue alloc] init];

Expand All @@ -69,25 +71,21 @@ - (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completio
if (!key) {
key = keyForURL(url);
}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSURLRequest* request = [NSURLRequest requestWithURL:url];

NSURLSessionDownloadTask *downloadImageTask = [privateSession downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^{

if(failure) failure(request, response, error);
});
if(failure){
dispatch_async(dispatch_get_main_queue(), ^{
failure(request, response, error);
});
}
return;
}

UIImage *i = [[UIImage alloc] initWithData:data];
if (!i)
NSData *data = [NSData dataWithContentsOfURL:location];
UIImage *downloadedImage = [UIImage imageWithData: data];
if (!downloadedImage)
{
NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:[NSString stringWithFormat:@"Failed to init image with data from for URL: %@", url] forKey:NSLocalizedDescriptionKey];
Expand All @@ -96,25 +94,26 @@ - (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completio

if(failure) failure(request, response, error);
});
}
else
{
}else{
NSString *cachePath = cachePathForKey(key);
NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(writeData:toPath:)]];

[writeInvocation setTarget:self];
[writeInvocation setSelector:@selector(writeData:toPath:)];
[writeInvocation setArgument:&data atIndex:2];
[writeInvocation setArgument:&cachePath atIndex:3];

[self performDiskWriteOperation:writeInvocation];
[self setImage:i forKey:key];

dispatch_async(dispatch_get_main_queue(), ^{
if(completion) completion(i);
});
[self setImage:downloadedImage forKey:key];
if(completion){
dispatch_async(dispatch_get_main_queue(), ^{
completion(downloadedImage);
});
}
}
});
}];

[downloadImageTask resume];
}

- (void) removeAllObjects {
Expand Down