iOS中多线程的实现方式及对比
来源:互联网 发布:m905 罗技 mac 编辑:程序博客网 时间:2024/06/14 22:27
- (void)loadView
{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, Screen_Width, Screen_Height)];
UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
downloadBtn.frame = CGRectMake(20.0f, StatusBar_Height + 80.0f, 200.0f, 40.0f);
downloadBtn.backgroundColor = OrangeColor;
[downloadBtn setTitle:@"下载"
forState:UIControlStateNormal];
[downloadBtn setTitle:@"下载"
forState:UIControlStateSelected];
[downloadBtn addTarget:self
action:@selector(download:)
forControlEvents:UIControlEventTouchUpInside];
[backgroundView addSubview:downloadBtn];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 140.0f, 280.0f, 300.0f)];
[backgroundView addSubview:self.imageView];
self.view = backgroundView;
}
- (void)download:(UIButton*)sender
{
NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];
NSThread *thread = [[NSThread alloc] initWithTarget:self
selector:@selector(downloadImage:)
object:url];
[thread start];
}
- (void)downloadImage:(NSURL*)url
{
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
if (data) {
UIImage *image = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:@selector(updateUI:)
withObject:image
waitUntilDone:YES];
}
}
- (void)updateUI:(UIImage*)image
{
self.imageView.image = image;
}
NSOperation的实现方式:
- (void)download:(UIButton*)sender
{
NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(downloadImage:)
object:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
- (void)downloadImage:(NSURL*)url
{
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
if (data) {
UIImage *image = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:@selector(updateUI:)
withObject:image
waitUntilDone:YES];
}
}
- (void)updateUI:(UIImage*)image
{
self.imageView.image = image;
}
GCD的实现方式:
- (void)download:(UIButton*)sender
{
//异步更新
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
if (data) {
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
});
}
});
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
//
// });
//dispatch_group_t 分组,将所有分组中的任务执行后,再更新界面
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"任务一");
});
dispatch_group_async(group, queue, ^{
NSLog(@"任务二");
});
dispatch_group_async(group, queue, ^{
NSLog(@"任务三");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//监测三组任务是否完全完成,完成后更新界面
});
//dispatch_barrier_async 表示前面的任务执行后,再执行dispatch_barrier_async 任务
dispatch_queue_t queue1 = dispatch_queue_create("com.liusanchun.gcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue1, ^{
NSLog(@"任务一");
});
dispatch_async(queue1, ^{
NSLog(@"任务二");
});
dispatch_barrier_async(queue1, ^{
NSLog(@"执行");
});
dispatch_async(queue1, ^{
NSLog(@"任务三");
});
}
- iOS中多线程的实现方式及对比
- iOS 多线程的实现方式及应用示例
- 多线程 +1操作的几种实现方式,及效率对比
- iOS中多线程的几种实现方式
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- ios多线程的对比
- Android中定时器实现的方式对比
- IOS多线程实现方式
- iOS 多线程的几种实现方式
- iOS多线程的简单实现方式
- iOS Swift多线程、互斥锁的实现方式
- 调和曲线图和轮廓图的比较
- java学习笔记--------第二天
- 跨浏览器CSS文本模糊效果
- 使用ffmpeg步骤
- PGA概念
- iOS中多线程的实现方式及对比
- 相关矩阵的可视化及其新方法探究
- TCP/IP网络层之 IP选路的实现过程
- 【DP】 HDU 3001 Travelling 3进制状压
- Java中实例方法与类方法的区别
- 《unix高级环境编程》信号——abort、system和sleep函数
- Swing中经常会遇到的若干问题——JTable(持续更新)
- lua基础【五】I/O文件操作
- Android 编程小提示Stability Tips