iOS 图片加载方法
2014-04-27 15:02
309 查看
(1)最简单的下载,显示图片的方法:
[plain] view
plaincopy
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)开辟线程来解决这个问题.
[plain] view
plaincopy
// 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:
[plain] view
plaincopy
#import <SDWebImage/UIImageView+WebCache.h>
[imageView setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
SDWebImage可以实现:
*下载和缓存图片.
*相同的url不会被重复下载.
*坏的url不会一直请求.
使用HJCache:
[plain] view
plaincopy
// 目前HJCache不支持ARC, 所以这是个问题.
-----------------------------------------------------------------------------------------------------------------
(二)多线程初步实现TableView的图片显示(之前用第三库老是不稳定) 这个算是比较满意的.
------------------------------------------------------------------------------------------------------------------------
[plain] view
plaincopy
@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];
}
[plain] view
plaincopy
queue = [[NSOperationQueue alloc] init];//这是成员队列的实例化
设置TableView cell中的图片:
[plain] view
plaincopy
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"];
}
[plain] view
plaincopy
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)开辟线程来解决这个问题.
[plain] view
plaincopy
// 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:
[plain] view
plaincopy
#import <SDWebImage/UIImageView+WebCache.h>
[imageView setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
SDWebImage可以实现:
*下载和缓存图片.
*相同的url不会被重复下载.
*坏的url不会一直请求.
使用HJCache:
[plain] view
plaincopy
// 目前HJCache不支持ARC, 所以这是个问题.
-----------------------------------------------------------------------------------------------------------------
(二)多线程初步实现TableView的图片显示(之前用第三库老是不稳定) 这个算是比较满意的.
------------------------------------------------------------------------------------------------------------------------
[plain] view
plaincopy
@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];
}
[plain] view
plaincopy
queue = [[NSOperationQueue alloc] init];//这是成员队列的实例化
设置TableView cell中的图片:
[plain] view
plaincopy
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"];
}
相关文章推荐
- iOS 加载图片方法imageNamed 与 imageWithContentsOfFile 比较
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- iOS加载图片imageNamed方法还是imageWithContentsOfFile
- flash ios 加载图片序列内存过大的解决方法
- iOS开发中用imageIO渐进加载图片及获取exif的方法
- iOS 开发之 为UIButton添加类别方法加载网络图片
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- iOS 开发之 为UIButton添加类别方法加载网络图片
- 【IOS】UIImage加载图片的方式以及Assets.xcassets对于加载方法的影响
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- SDWebimage 加载大量图片的时候内存泄露的解决方法(ios自学笔记)
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- iOS异步加载图片方法
- iOS 同一页面加载上百张图片,迅速滑动时导致内存暴涨程序崩溃的参考解决方法
- IOS开发中加载大量网络图片优化方法
- iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
- iOS 根据不同的系统版本加载不同图片方法