这里需要创建一个imageCacheObject对象。存放一些缓存数据

      一个imageViewController视图。展示下载的图片

      一个imagesTableViewController表格。展示可下载的图片

****************************imageCacheObject.h*********************************************


#import <Foundation/Foundation.h>

@interface ImageCacheObject : NSObject
//用于存放所有图片信息
@property (nonatomic,strong) NSMutableDictionary * imageCacheDic;
//单例 方法保证整个进程仅有一份
+(id)shareImageCache;

@end

******************************imageCacheObject.m*******************************************

#import "ImageCacheObject.h"

@implementation ImageCacheObject

+ (id)shareImageCache{
    static ImageCacheObject * obj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        obj = [ImageCacheObject new];
    });
    return obj;
}

-  (instancetype)init{
    if (self =[super init]) {
        self.imageCacheDic = [NSMutableDictionary new];
    }
    return  self;
}

@end

******************************imageViewController.m*******************************************

在.h文件中申明一个 nsstring * imagePath注意和.m文件中的imagesPath分开

 

#import "ImageViewController.h"
#import "ImageCacheObject.h"
@interface ImageViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;
@property (nonatomic,strong) UIImageView *imageView;
//获取图片存放的地址
@property (nonatomic,strong) NSString * imagesPath;

@end

@implementation ImageViewController

- (NSString *)imagesPath{
    if (!_imagesPath) {
        NSString * documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        _imagesPath = [documentPath stringByAppendingPathComponent:@"images"];
        NSLog(@"%@",_imagesPath);
        //需要判断文件夹是否存在,如果不存在需要创建一个
        NSFileManager * manager =[NSFileManager defaultManager];
        if (![manager fileExistsAtPath:self.imagesPath]) {
            [manager createDirectoryAtPath:self.imagesPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    return _imagesPath;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //先通过路径从单例的字典中获取值,如果有值说明图片下载过,不需要重复下载
    ImageCacheObject * cacheObj = [ImageCacheObject shareImageCache];
    UIImage * img = [cacheObj.imageCacheDic objectForKey:self.imagePath];
    if (img) {
        UIImageView * imageView1 = [[UIImageView alloc]initWithImage:img];
        [self.scrollerView addSubview:imageView1];
        self.scrollerView.contentSize = img.size;
        
        UIPinchGestureRecognizer * pgc = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scalechange:)];
        
        [self.view addGestureRecognizer:pgc];

        return;//让代码停住,不继续执行
    }
    
    //nsstring - url - data -uiimage
    //**/**/photo_1.jpg -> ~/Documents/images/pthot_1.jpg
    NSString * lastcomponent = self.imagePath.lastPathComponent;//获取网络地址的最后一段。
    NSString * filePath = [self.imagesPath stringByAppendingPathComponent:lastcomponent];
    NSFileManager * manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        UIImage *img = [UIImage imageWithContentsOfFile:filePath];
        UIImageView * imageview = [[UIImageView alloc]initWithImage:img];
        [self.scrollerView addSubview:imageview];
        self.scrollerView.contentSize =img.size;
        return;
    }
    
    
    
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    [self.activityView startAnimating];// 开始读取动画
    //在状态栏上显示网络活动,同时要在下载完成后结束该方法。
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];;
    
    dispatch_async(globalQueue, ^{
        NSURL * imageURL = [NSURL URLWithString:self.imagePath];
        NSData *data = [NSData dataWithContentsOfURL:imageURL];
        UIImage * image = [UIImage imageWithData:data];
        [data writeToFile:filePath atomically:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.activityView stopAnimating]; //结束读取动画
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            UIImageView * imageView = [[UIImageView alloc]initWithImage:image];
            self.imageView = imageView;
            [self.scrollerView addSubview:imageView];
            self.scrollerView.contentSize = image.size;
            //图片加载完成就添加到单例的字典中保存起来,保存的时间是整个进程。
            ImageCacheObject *obj = [ImageCacheObject shareImageCache];
            [obj.imageCacheDic setObject:image forKey:self.imagePath];
        });
    });
    
    
    UIPinchGestureRecognizer * pgc = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scalechange:)];
    
    [self.view addGestureRecognizer:pgc];

   }

-(void)scalechange:(UIPinchGestureRecognizer *)pgc{

    CGFloat scale = pgc.scale;
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);
    CGFloat max = MAX((self.imageView.bounds.size.width/self.view.bounds.size.width), (self.imageView.bounds.size.height/self.view.bounds.size.height));
    self.scrollerView.minimumZoomScale = 1/max;
    
    pgc.scale =1;
}



@end

********************************imagesTableViewController.m*****************************************

#import "ImagesTableViewController.h"
#import "ImageViewController.h"
@interface ImagesTableViewController ()
@property (nonatomic,strong) NSArray * imageNames;
@end

@implementation ImagesTableViewController


//筹备用于展示的数据
- (NSArray *)imageNames{
    if (!_imageNames) {
        _imageNames =@[@"鲜花",@"彩椒",@"水母",@"日落",@"孩子",@"滑板少年"];
    }
    return _imageNames;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
    
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.imageNames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.imageNames[indexPath.row];
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //下面方法让选中的行恢复到原来样子,取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//通过点击Cell跳转,如果是storyboard连线模式,会自动触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    //用户点击Cell发生的跳转,所以sender就是Cell
    //获取当前Cell所在的行
    NSIndexPath * indexPath = [self.tableView indexPathForCell:sender];
    NSString * imagePath = [NSString stringWithFormat:@"http://images.apple.com/v/iphone-5s/gallery/a/images/download/photo_%ld.jpg",indexPath.row+1];
    ImageViewController * imagevc = segue.destinationViewController;
    imagevc.imagePath =imagePath;

}


@end

*************************************************************************