一.NSThread线程间通信
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建我们的UIImageView
[self createUIImageView];
//2.去网络上下载图片
NSString *URLString = @"http://e.hiphotos.bdimg.com/album/pic/item/9345d688d43f8794f0ab07ddd01b0ef41ad53ae8.jpg";
[self performSelectorInBackground:@selector(loadImage:) withObject:URLString];
//3.设置最大最小缩放比例
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.minimumZoomScale = 0.5;
}
/**
去子线程下载图片
*/
- (void)loadImage:(NSString *)URLString{
NSLog(@"---loadImage start---");
//1.生成NSURL
NSURL *url = [NSURL URLWithString:URLString];
//2.去网络上下载,网络上传输的都是一堆01010的二进制
NSData *imageData = [NSData dataWithContentsOfURL:url];
//3.将我们的imageData转成我们UIImage
UIImage *image = [UIImage imageWithData:imageData];
/**
waitUntilDone 等我们的updateUI 方法执行完毕之后,再执行后面的
*/
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:NO];
NSLog(@"---loadImage end---");
}
- (void)updateUI:(UIImage *)image{
//1.将image设置给imageView
self.imageView.image = image;
//2.根据我们的图片大小,来决定我们UIImageView的尺寸
[self.imageView sizeToFit];
//3.设置scrollView的contentSize
self.scrollView.contentSize = image.size;
NSLog(@"update UI end");
}
- (void)createUIImageView{
UIImageView *imageView =[[UIImageView alloc] init];
[self.scrollView addSubview:imageView];
self.imageView = imageView;
}
#pragma mark - scrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
}
@end
二.GCD线程间通信
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建我们的UIImageView
[self createUIImageView];
//2.去网络上下载图片
/**
去子线程,并发,全局,串行 方法:异步
两个参数
参数1:队列
参数2:任务
*/
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"loadImage----%@",[NSThread currentThread]);
//0.URLString
NSString *URLString = @"http://e.hiphotos.bdimg.com/album/pic/item/9345d688d43f8794f0ab07ddd01b0ef41ad53ae8.jpg";
//1.生成NSURL
NSURL *url = [NSURL URLWithString:URLString];
//2.去网络上下载,网络上传输的都是一堆01010的二进制
NSData *imageData = [NSData dataWithContentsOfURL:url];
//3.将我们的imageData转成我们UIImage
UIImage *image = [UIImage imageWithData:imageData];
//去主线程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"---updateUI---%@",[NSThread currentThread]);
//1.将image设置给imageView
self.imageView.image = image;
//2.根据我们的图片大小,来决定我们UIImageView的尺寸
[self.imageView sizeToFit];
//3.设置scrollView的contentSize
self.scrollView.contentSize = image.size;
});
});
//3.设置最大最小缩放比例
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.minimumZoomScale = 0.5;
}
- (void)createUIImageView{
//1.创建imageView
UIImageView *imageView =[[UIImageView alloc] init];
[self.scrollView addSubview:imageView];
self.imageView = imageView;
}
#pragma mark - scrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
}
@end
三.NSOperation线程间通信
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1j.创建我们的UIImageView
[self createUIImageView];
//2.去网络上下载图片
//先去子线程做耗时操作(下载) 然后再去主线程更新UI
//创建并发队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
//子线程
NSLog(@"loadImage----%@",[NSThread currentThread]);
//0.URLString
NSString *URLString = @"http://e.hiphotos.bdimg.com/album/pic/item/9345d688d43f8794f0ab07ddd01b0ef41ad53ae8.jpg";
//1.生成NSURL
NSURL *url = [NSURL URLWithString:URLString];
//2.去网络上下载,网络上传输的都是一堆01010的二进制
NSData *imageData = [NSData dataWithContentsOfURL:url];
//3.将我们的imageData转成我们UIImage
UIImage *image = [UIImage imageWithData:imageData];
//去主线程更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//主线程
NSLog(@"---updateUI---%@",[NSThread currentThread]);
//1.将image设置给imageView
self.imageView.image = image;
//2.根据我们的图片大小,来决定我们UIImageView的尺寸
[self.imageView sizeToFit];
//3.设置scrollView的contentSize
self.scrollView.contentSize = image.size;
}];
}];
//3.设置最大最小缩放比例
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.minimumZoomScale = 0.5;
}
- (void)createUIImageView{
UIImageView *imageView =[[UIImageView alloc] init];
[self.scrollView addSubview:imageView];
self.imageView = imageView;
}
#pragma mark - scrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
}
@end