GCD下载保存图片 缓存

//

//  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:完成");

    }];



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值