图片下载缓存模块大概的逻辑
实现XXPictureLoader类:
(1)- (NSData *)loadPictureSync:(NSString *)url; //同步加载
(2)- (void) loadPicktureAsync:(NSString *)url; //异步加载
异步加载要设计delegate回调接口,要求返回请求结果数据,或者失败原因。
另,在沙箱/tmp/中创建“pcache”目录,加载的图片数据以url的crc32串保存到该目录,Loader类每次加载图片时先去该目录查找是否有缓存,若有则直接返回无需网络下载,没有再去网络加载。
//
// ViewController.h
// XXPictrueLoader
//
// Created by chengen on 13-10-29.
// Copyright (c) 2013年 chengen. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSURLConnectionDelegate>
-(NSData*) loadPictureSync:(NSString*)url;
-(void)loadPicktureAsync:(NSString*)url;
@end
//
// ViewController.m
// XXPictrueLoader
//
// Created by chengen on 13-10-29.
// Copyright (c) 2013年 chengen. All rights reserved.
//
#import "ViewController.h"
#import "curl/curl.h"
#define POSTURL "http://192.168.1.99/xampp/test/"
@interface ViewController ()
{
NSURLConnection *connection; //连接网络
NSMutableData *allData; //下载的数据
NSData *data1;
UIView *_view;
UIImageView *_imageView;
NSData *data2;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_view = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 200)];
_view.backgroundColor =[UIColor clearColor];
[self.view addSubview:_view];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 300, 200, 150)];
[button setTitle:@"按钮" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 150, 80, 50)];
[button1 setTitle:@"按钮" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor blueColor];
[self.view addSubview:button1];
[button addTarget:self action:@selector(loadPictureSync:) forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self action:@selector(loadPicktureAsync:) forControlEvents:UIControlEventTouchUpInside];
//在tmp下创建pcache目录
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"temp目录:%@",tempPath);
NSString *filename = [tempPath stringByAppendingPathComponent:@"pcache"];
NSFileManager *fileMgr = [NSFileManager defaultManager];
[fileMgr createDirectoryAtPath:filename attributes:nil];
NSLog(@"tmp:%@",fileMgr);
}
-(NSData*) loadPictureSync:(NSString*)url //同步下载
{
NSError *error = nil;
NSString * urlString=@"http://g.hiphotos.baidu.com/album/w%3D2048/sign=4a4c7601d31b0ef46ce89f5ee9fc50da/f636afc379310a555d97e0dbb64543a982261069.jpg";
NSURL *url1 = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url1];
data1 = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if (data1) {
NSLog(@"111");
// UIImage *image = [UIImage imageWithData:data];
// UIImage *image = [UIImage imageNamed:data];
}
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
[_imageView setImage:[UIImage imageWithData:data1]];
_imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:_imageView];
[request release];
return data1;
}
-(void)loadPicktureAsync:(NSString*)url //异步下载
{
if (connection!=nil) {
[connection release];
}
if (allData!=nil) {
[allData release];
}
NSString * urlString=@"http://g.hiphotos.baidu.com/album/w%3D2048/sign=4a4c7601d31b0ef46ce89f5ee9fc50da/f636afc379310a555d97e0dbb64543a982261069.jpg";
NSURL *url1 = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url1 cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:50.0]; //通过url,获得request;
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //根据request 获取connection
[request release];
}
//1、接收完HTTP协议头,开始真正接手数据时候调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
allData = [[NSMutableData alloc]initWithLength:0];
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
NSInteger statuCode = [res statusCode];
if (statuCode>=400) {
NSLog(@"HTTP ERROR CODE %ld",(long)statuCode);
}
NSDictionary *dic = [res allHeaderFields];
NSLog(@"all Headr field%@",dic);
}
//2、每接收一段数据就会调用此函数
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[allData appendData:data];//把data加到allData最后
}
//3、接收数据失败时调用,并且中断下载
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
//4、最后,如果连接成功并下载,会调用,一般在这释放一些alloc创建的内存 可以对一些数据进行处理,如在这里就用data得到图片;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *string = [[NSString alloc]initWithData:allData encoding:NSUTF8StringEncoding];
NSLog(@"string----->%@",string);
NSLog(@"functiono %s is calling",__func__); //获得方法名称
UIImage *image = [UIImage imageWithData:allData];
UIImageView *_imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 80, 80)];
[_imageView1 setImage:image];
[self.view addSubview:_imageView1];
[_imageView1 release];
[string release];
[allData release];
}
-(void)writeToFile:(NSData *)data //接收得数据写入文件
{
// //在tmp下创建pcache目录
// NSString *tempPath = NSTemporaryDirectory();
// NSLog(@"temp目录:%@",tempPath);
// NSString *filename = [tempPath stringByAppendingPathComponent:@"pcache"];
// NSFileManager *fileMgr = [NSFileManager defaultManager];
// [fileMgr createDirectoryAtPath:filename attributes:nil];
// NSLog(@"tmp:%@",fileMgr);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end