//
// UIImage+ResizeImage.h
//
#import <UIKit/UIKit.h>
@interface UIImage (ResizeImage)
- (CGSize)calculateResize:(CGSize)size;
- (UIImage *)resize:(CGSize)size;
@end
//
// UIImage+ResizeImage.m
//
#import "UIImage+ResizeImage.h"
@implementation UIImage (ResizeImage)
- (CGSize)calculateResize:(CGSize)size
{
CGFloat imageWidth=self.size.width;
CGFloat imageHeight=self.size.height;
CGFloat width,height;
width=imageWidth;
if (width>size.width) {
width=size.width;
}
height=(imageHeight/imageWidth) * (width);
if (height>size.height) {
height=size.height;
width=(imageWidth/imageHeight) *(height);
}
CGSize resize=CGSizeMake(width, height);
return resize;
}
- (UIImage *)resize:(CGSize)size
{
CGSize resize=[self calculateResize:size];
UIGraphicsBeginImageContext(CGSizeMake(resize.width, resize.height));
[self drawInRect:CGRectMake(0, 0, resize.width, resize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
@end
//
// UIImageView+DispatchLoad.h
//
#import <UIKit/UIKit.h>
@interface UIImageView (DispatchLoad)
- (void)setImageForPath:(NSString *)path completion:(void(^)(void))completion;
@end
//
// UIImageView+DispatchLoad.m
//
#import "UIImageView+DispatchLoad.h"
#import "UIImage+ResizeImage.h"
@implementation UIImageView (DispatchLoad)
- (void)setImageForPath:(NSString *)path completion:(void(^)(void))completion
{
//地址或文件明是否有效
if (!path) {
return;
}
//是否以url 开始
if ([path hasPrefix:@"http://"])
{
//本地是否存在
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/ImageCaches",documentPath];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSArray *filePathArray = [path componentsSeparatedByString:@"/"];
NSString *lastName = nil;
if ([filePathArray count] > 0)
{
lastName = [filePathArray lastObject];
}
filePath = [filePath stringByAppendingFormat:@"/%@",lastName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
//读本地的
NSLog(@"根据url 读取本地文件成功");
UIImage *imageData = [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]];
self.image = [imageData resize:self.frame.size];
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"开始下载:%@",path);
UIImage *data = nil;
NSURL *url = [NSURL URLWithString:path];
NSData *responseData = [NSData dataWithContentsOfURL:url];
if ([responseData writeToFile:filePath atomically:YES])
{
NSLog(@"保存文件成功");
}
data = [UIImage imageWithData:responseData];
if (data)
{
NSLog(@"下载成功");
dispatch_async(dispatch_get_main_queue(), ^{
self.image = [data resize:self.frame.size];
});
dispatch_async(dispatch_get_main_queue(), completion);
}else{
NSLog(@"下载失败");
}
});
}
//根据文件名 直接读本地
}else{
UIImage *imageData = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:nil]];
self.image = [imageData resize:self.frame.size];
}
}
@end
调用
[self.showImageView setImageForPath:@"http://news.cnr.cn/native/pic/201312/W020131203250477363011.jpg" completion:^{
NSLog(@"challengeMethod:完成");
}];