From e90619b9d79758cbc01b6169c8912e00b4af9530 Mon Sep 17 00:00:00 2001 From: Xeis-Studio Date: Tue, 2 Aug 2016 11:37:04 -0500 Subject: [PATCH 1/2] =?UTF-8?q?Correcci=C3=B3n=20de=20imports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Se añade el import de la biblioteca de criptografia y se define método de encripcion comp público --- JMImageCache.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/JMImageCache.h b/JMImageCache.h index f527da0..5650827 100644 --- a/JMImageCache.h +++ b/JMImageCache.h @@ -7,6 +7,7 @@ // #import "UIImageView+JMImageCache.h" +#import @class JMImageCache; @@ -18,7 +19,9 @@ @end -@interface JMImageCache : NSCache +@interface JMImageCache : NSCache{ + NSURLSession *privateSession; +} + (JMImageCache *) sharedCache; @@ -42,4 +45,6 @@ - (void) writeData:(NSData *)data toPath:(NSString *)path; - (void) performDiskWriteOperation:(NSInvocation *)invoction; ++ (NSString *)SHA1FromString:(NSString *)string; + @end From c9c750ff4e5f92ebc42f8f2c00b40bca479059f8 Mon Sep 17 00:00:00 2001 From: Xeis-Studio Date: Tue, 2 Aug 2016 11:38:24 -0500 Subject: [PATCH 2/2] Cambio de NSURLConnection a NSURLSession Se cambia el uso de NSURLConnection por NSURLSession para el soporte de IPv6 obligado por apple en iOS 9 --- JMImageCache.m | 51 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/JMImageCache.m b/JMImageCache.m index 5920e93..7db4bb1 100644 --- a/JMImageCache.m +++ b/JMImageCache.m @@ -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]; @@ -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]; @@ -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 {