| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- //
- // LeslieAsyncImageDownloader.m
- // AsyncUIImageView
- //
- // Created by Leslie.Fang on 14-8-11.
- // Copyright (c) 2017年 Enway. All rights reserved.
- //
- #import "LeslieAsyncImageDownloader.h"
- @implementation LeslieAsyncImageDownloader
- + (id)sharedImageLoader{
- static LeslieAsyncImageDownloader *sharedImageLoader = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedImageLoader = [[self alloc] init];
- });
-
- return sharedImageLoader;
- }
- - (void)downloadImageWithMultipleURL:(NSURL *)url complete:(ImageDownloadedBlock)completeBlock{
- LeslieImageCache *imageCache = [LeslieImageCache sharedCacheMultiple];
- NSString *imageUrl = [url absoluteString];
- // UIImage *image = [imageCache getImageFromMemoryForkey:imageUrl];
- // // // 先从内存中取
- // if (image) {
- // if (completeBlock) {
- // //NSLog(@"image exists in memory");
- // completeBlock(image,nil,url);
- // }
- //
- // return;
- // }
-
- // 再从文件中取
- // image = [imageCache getImageFromFileForKey:imageUrl];
- // if (image) {
- // if (completeBlock) {
- // //NSLog(@"image exists in file");
- // completeBlock(image,nil,url);
- // }
- //
- // // 重新加入到 NSCache 中
- // [imageCache cacheImageToMemory:image forKey:imageUrl];
- //
- // return;
- // }
-
- // 内存和文件中都没有再从网络下载
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
- NSError * error;
- NSData *imgData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
- dispatch_async(dispatch_get_main_queue(), ^{
- UIImage *image = [UIImage imageWithData:imgData];
- if (image) {
- // 先缓存图片到内存
- // [imageCache cacheImageToMemory:image forKey:imageUrl];
-
- // 再缓存图片到文件系统
- NSString *extension = [[imageUrl substringFromIndex:imageUrl.length-3] lowercaseString];
- NSString *imageType = @"jpg";
-
- if ([extension isEqualToString:@"jpg"]) {
- imageType = @"jpg";
- }else{
- imageType = @"png";
- }
-
- [imageCache cacheImageToFile:image forKey:imageUrl ofType:imageType];
- }
-
- if (completeBlock) {
- completeBlock(image,error,url);
- }
- });
- });
- }
- - (void)downloadImageWithURL:(NSURL *)url complete:(ImageDownloadedBlock)completeBlock{
- LeslieImageCache *imageCache = [LeslieImageCache sharedCache];
- NSString *imageUrl = [url absoluteString];
- UIImage *image = [imageCache getImageFromMemoryForkey:imageUrl];
- // // 先从内存中取
- if (image) {
- if (completeBlock) {
- NSLog(@"image exists in memory");
- completeBlock(image,nil,url);
- }
-
- return;
- }
-
- // 再从文件中取
- image = [imageCache getImageFromFileForKey:imageUrl];
- if (image) {
- if (completeBlock) {
- NSLog(@"image exists in file");
- completeBlock(image,nil,url);
- }
-
- // 重新加入到 NSCache 中
- [imageCache cacheImageToMemory:image forKey:imageUrl];
-
- return;
- }
- // 内存和文件中都没有再从网络下载
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
- NSError * error;
- NSData *imgData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
- dispatch_async(dispatch_get_main_queue(), ^{
- UIImage *image = [UIImage imageWithData:imgData];
- if (image) {
- // 先缓存图片到内存
- [imageCache cacheImageToMemory:image forKey:imageUrl];
-
- // 再缓存图片到文件系统
- NSString *extension = [[imageUrl substringFromIndex:imageUrl.length-3] lowercaseString];
- NSString *imageType = @"jpg";
-
- if ([extension isEqualToString:@"jpg"]) {
- imageType = @"jpg";
- }else{
- imageType = @"png";
- }
-
- [imageCache cacheImageToFile:image forKey:imageUrl ofType:imageType];
- }
-
- if (completeBlock) {
- completeBlock(image,error,url);
- }
- });
- });
- }
- -(void)downloadFileWithURL:(NSURL *)url filePath:(NSString*)path
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
- NSError * error;
- NSData *fileData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
- dispatch_async(dispatch_get_main_queue(), ^{
-
- if(fileData)
- {
- [fileData writeToFile:path atomically:YES];
-
- }
-
-
- });
- });
- }
- + (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
- {
- //先调整分辨率
- CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
-
- CGFloat tempHeight = newSize.height / 1024;
- CGFloat tempWidth = newSize.width / 1024;
-
- if (tempWidth > 1.0 && tempWidth > tempHeight) {
- newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
- }
- else if (tempHeight > 1.0 && tempWidth < tempHeight){
- newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
- }
-
- UIGraphicsBeginImageContext(newSize);
- [source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- //调整大小
- NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);
- NSUInteger sizeOrigin = [imageData length];
- NSUInteger sizeOriginKB = sizeOrigin / 1024;
- if (sizeOriginKB > maxSize) {
- NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.5);
- return finallImageData;
- }
-
- return imageData;
- }
- @end
|