ios 多线程加载图片
2015-05-29 10:09
351 查看
NSThread异步加载图片
NSOperationQueue异步加载图片
-(void)main{
if (![self isCancelled]) {
NSLog(@"start");
NSData *data = [NSURLConnection sendSynchronousRequest:self.request returningResponse:nil error:nil];
self.image = [[UIImage imageWithData:data]retain];
if (self.delegate) {
[self.delegate imageLoadedForIndexPath:self.indexPath withImage:self.image];
}
}
}
GCD异步加载图片
//启动线程,下载图片,下载完成后放入缓存数组中
dispatch_async(dispatch_get_global_queue(0, 0), ^{
……
UIImage * image = [UIImage imageWithData:data];
if(image != nil){
[self.imagesCache replaceObjectAtIndex:indexPath.row withObject:image];
}
//下载完成后转到主线程更新表格cell
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImageView * imageView = (UIImageView *)[cell.contentView viewWithTag:111];
imageView.image = [self.imagesCache objectAtIndex:indexPath.row];
});
});
-(void)downloadImage:(NSIndexPath *)indexPath { @autoreleasepool { …… UIImage * image = [UIImage imageWithData:data]; if(image != nil){ [self.imagesCache replaceObjectAtIndex:indexPath.row withObject:image]; } //更新UI的代码须到主线程中执行 [self performSelectorOnMainThread:@selector(updateTableViewCellImage:) withObject:indexPath waitUntilDone:NO]; } }
NSOperationQueue异步加载图片
-(void)main{
if (![self isCancelled]) {
NSLog(@"start");
NSData *data = [NSURLConnection sendSynchronousRequest:self.request returningResponse:nil error:nil];
self.image = [[UIImage imageWithData:data]retain];
if (self.delegate) {
[self.delegate imageLoadedForIndexPath:self.indexPath withImage:self.image];
}
}
}
// Configure the cell... UIImage *image = (UIImage *)[self.imgDic objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]]; if (!image) { image = self.avatarImage; [self.imgDic setObject:image forKey:[NSString stringWithFormat:@"%d",indexPath.row]]; //起一个线程,去下载图片 ImageLoader *loader = [[[ImageLoader alloc]initWithUrl:[self.sourceUrlArray objectAtIndex:indexPath.row] andIndexPath:indexPath]autorelease]; loader.delegate = self; [self.queue addOperation:loader]; } cell.imageView.image = image;
GCD异步加载图片
//启动线程,下载图片,下载完成后放入缓存数组中
dispatch_async(dispatch_get_global_queue(0, 0), ^{
……
UIImage * image = [UIImage imageWithData:data];
if(image != nil){
[self.imagesCache replaceObjectAtIndex:indexPath.row withObject:image];
}
//下载完成后转到主线程更新表格cell
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImageView * imageView = (UIImageView *)[cell.contentView viewWithTag:111];
imageView.image = [self.imagesCache objectAtIndex:indexPath.row];
});
});
相关文章推荐
- iOS开发宝典:String用法大全
- iOS集合
- 实现iOS应用图标右上角数字badge number提醒
- iOS中的排序笔记
- 类似iOS自带相册应用的图片浏览的实现
- IOS相册功能
- IOS如何实现灵活的瀑布流(2)
- iOS端项目注释规范附统一代码块
- IOS平台生成图片缩略图的两种方案
- ios日期格式yyyy-MM-dd
- ios 多线程 NSThread Cocoa NSOperation GCD
- IOS CoreText --- 图文混排
- IOS CoreText --- 段落样子CTParagraphStyle
- IOS CoreText --- 行 CTLineRef
- IOS CoreText --- 基本用法
- ios 手势
- IOS 开发学习28 NSArray 基本用法
- iOS 逆向之ARM汇编
- iOS开发笔记18--SEL (@selector) 原理及使用总结(二)
- iOS开发笔记17-SEL (@selector) 原理及使用总结(一)