ios中子线程如何加载图片
来源:互联网 发布:自动顶贴软件 编辑:程序博客网 时间:2024/06/05 10:05
方法一:
GCD
//在主线程里调用,然后跳到一个子线程。
dispatch_async(dispatch_get_global_queue(0,0), ^{
//IMAGE_URL为定位的宏。为图片的地址
//在子线程里完成对网上图片的解析
NSURL *imageURL = [NSURL URLWithString:IMAGE_URL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
//完成解析后然后跳到主线程里执行UIImageView的操作。
dispatch_async(dispatch_get_main_queue(), ^{
//创建一个UIImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 460)];
//对UIImageView加载下载好的图片。
imageView.image = [UIImageimageWithData:imageData];
[self.windowaddSubview:imageView];
[imageViewrelease];
});
});
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(sonThreah) object:nil];
启动子线程
[thread start];
//实现子线程里的方法
-(void)sonThreah{
//跳到子线程,对图片进行解析
NSURL *imageURL = [NSURL URLWithString:IMAGE_URL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
//解析完跳到主线程
[selfperformSelectorOnMainThread:@selector(showImage:)withObject:imageData waitUntilDone:NO];
}
-(void)showImage:(NSData *)data {
//主线程完成对图片的加载
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 460)];
[self.windowaddSubview:imageView];
imageView.image = [UIImageimageWithData:data];
[imageView release];
}
- ios中子线程如何加载图片
- IOS中子线程刷新UI
- html文件如何加载iOS本地图片
- iOS开发中如何解决TableView中图片延时加载
- iOS开发中如何解决TableView中图片延时加载
- IOS中如何异步加载图片(三)KVO观察者
- iOS原生如何加载HTML中img标签的图片
- iOS开发中如何解决TableView中图片延时加载
- IOS中如何异步加载图片(二)异步加载图片
- iOS异步加载图片
- iOS延时加载图片
- iOS 异步加载图片
- iOS 图片加载方法
- IOS 图片加载封装
- ios 多线程加载图片
- iOS异步加载图片
- iOS动画加载图片
- ios加载gif图片
- HDOJ搜索题(总计100题)
- nyoj,283,对称排序
- Linux基础编程-文件操作open,lseek,read,write,close
- 关于/usr/bin/env
- Android中文语音合成(TTS)
- ios中子线程如何加载图片
- 并查集
- 版本管理
- alert() confirm() prompt() 之用法与区别
- ural 1019 Line Painting (线段树)
- gcc选项
- Java基础知识(1):for/in语句
- SRM 451 div1 (practice)
- Bitmap ---Bitmap.createBitmap函数有6个重载方法