iOS圖片的下載緩存全部在此

注意: 我的文章只寫給自己看

----------------------------------------------------------------------------------------

(一)這部分(感覺out了), 但是還是保留,  算是學習的痕跡.

----------------------------------------------------------------------------------------

(1)最簡單的下載,顯示圖片的方法: 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[self.view bounds]];
    imageView.image = [self loadImageFromUrl:@"http://storage.live.com/items/72A00BF5A838647C!1616?filename=meinv004.jpg"];
    [self.view addSubview:imageView];
    
    -(UIImage*)loadImageFromUrl: (NSString*)url
	{
    	NSURL  *imageUrl = [NSURL URLWithString:url];
    	NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
    	UIImage *image = [UIImage imageWithData:imageData];
    	return image;
	}
    
這種最簡單的圖片加載方式阻塞了main線程. 使得流程不能流暢進行.


(2)開闢線程來解決這個問題.

    // set imageview
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[self.view bounds]];
    imageView.backgroundColor = [UIColor yellowColor];
    imageView.tag = imageView_tag;
    [self.view addSubview:imageView];
    
    // load image in background
    NSString *url = IMAGE_URL;
    [self performSelectorInBackground:@selector(loadImageFromUrl:) withObject:url];
    


	-(void)loadImageFromUrl: (NSString*)url {
    	NSURL  *imageUrl = [NSURL URLWithString:url];
    	NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
    	[self performSelectorOnMainThread:@selector(updateImageView:) withObject:imageData waitUntilDone:NO];
	}
	-(void) updateImageView:(NSData*) data {
    	UIImageView *imageView = (UIImageView *)[self.view viewWithTag:imageView_tag];
    	imageView.image = [UIImage imageWithData:data];
	}


並且只能在main線程中設置UI的內容, 所以代碼量增加了較多. 代碼量暫且不管, 這裏還有一個比較嚴重的問題就是每次都要加載圖片, 


使用SDWebImage: 
	#import <SDWebImage/UIImageView+WebCache.h>
	[imageView setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
SDWebImage可以實現: 
*下載和緩存圖片.
*相同的url不會被重複下載.

*壞的url不會一直請求.


使用HJCache:

                 // 目前HJCache不支持ARC, 所以這是個問題.


-----------------------------------------------------------------------------------------------------------------

(二)多線程初步實現TableView的圖片顯示(之前用第三庫老是不穩定) 這個算是比較滿意的.

------------------------------------------------------------------------------------------------------------------------

@interface c:NSOperation

@property NSString *url;
@property NSString *imageName;
@property UIImage *image;
@property UIImageView *delegate;

-(void) main;
-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate;

@end

@implementation c:NSOperation
@synthesize url = _url,imageName=_imageName, image=_image, delegate=_delegate;

-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate{
    if (self = [super init]) {
        self.url = url;
        self.imageName = imageName;
        self.delegate = delegate;
    }
    return self;
}

-(void) main{
    //
    NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: self.imageName];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]];
    [data writeToFile:cachefile atomically:YES];
    
    //
    self.image = [UIImage imageWithData:data];
    [self performSelectorOnMainThread:@selector(u) withObject:nil waitUntilDone:NO];
}
-(void)u{
    [self.delegate setImage:self.image];
}

    queue = [[NSOperationQueue alloc] init];//這是成員隊列的實例化

設置TableView cell中的圖片:

    NSString *filename = [NSString stringWithFormat:@"%d", indexPath.row];
    NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: filename];
    UIImage *image = [UIImage imageWithContentsOfFile:cachefile];
    if (image) {
        cell.imageView.image = image;
    } else {
        c *o = [[c alloc] initWith:[_objects objectAtIndex:indexPath.row] imageName:[NSString stringWithFormat:@"%d",indexPath.row] delegate:cell.imageView];
        [queue addOperation:o];
        cell.imageView.image= [UIImage imageNamed:@"placeholder.png"];
    }


注: 保存一下測試圖片  urls



















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章