// // ImageHandler.m // TumblrDownloader // // Created by HonorLee on 16/1/21. // Copyright © 2016年 HonorLee. All rights reserved. // #import "ImageHandler.h" @implementation ImageHandler +(NSString *)getAndSaveThumbImageFromURLandSetToImageView:(NSString *)imagePath saveToName:(NSString *)fileName setToImage:(UIImageView *)imageView{ UIImage *oldImg; UIImage *newImg; NSData *imgData; NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Library/Caches/%@.jpg",fileName]]; NSLog(@"%@",filePath); BOOL needSave = NO; BOOL onError = NO; NSError * error; dispatch_group_t disG = dispatch_group_create(); dispatch_group_enter(disG); if([imagePath containsString:@"http"]){ needSave = YES; imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imagePath]]; }else{ imgData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error]; } if(error) { NSLog(@"%@",error); } if(imgData){ if(needSave){ oldImg = [UIImage imageWithData:imgData]; UIGraphicsBeginImageContext(CGSizeMake(60, 60)); [oldImg drawInRect:CGRectMake(0, 0, 60, 60)]; newImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }else{ NSLog(@"%@",imgData); newImg = [UIImage imageWithData:imgData]; } }else{ onError = YES; } dispatch_group_leave(disG); dispatch_group_notify(disG, dispatch_get_main_queue(), ^{ if(!onError) [imageView setImage:newImg]; NSLog(@"123"); if(needSave){ // [UIImageJPEGRepresentation(newImg, 1.0) writeToFile:filePath options:nil error:&error atomically:YES]; NSFileManager *fs = [NSFileManager defaultManager]; BOOL isDir; [fs fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Caches"] isDirectory:&isDir]; NSLog(@"%@",isDir); NSError *err; [UIImageJPEGRepresentation(newImg, 1.0) writeToFile:filePath options:NSDataWritingAtomic error:&err]; NSLog(@"%@",err); } }); return filePath; } @end