IOS7笔记-10、多线程、滚动视图
1、滚动视图使用方方法:
1 @interface ImageViewController ()<UIScrollViewDelegate>//声明委托 2 @property (strong, nonatomic) UIImageView *imageView; 3 @property (strong, nonatomic) UIImage *image; 4 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; 5 @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner; 6 @end 7 8 -(void)setScrollView:(UIScrollView *)scrollView 9 { 10 _scrollView = scrollView; 11 _scrollView.minimumZoomScale = 0.2; //设置缩放特性 12 _scrollView.maximumZoomScale = 2.0; //设置缩放特性 13 _scrollView.delegate = self; //设置委托 14 self.scrollView.contentSize = self.image ? self.image.size : CGSizeZero; //设置滚动视图可移动特性 15 } 16 17 //实现缩放委托 18 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 19 { 20 return self.imageView; 21 } 22 23 -(UIImageView *)imageView 24 { 25 if (!_imageView) { 26 _imageView = [[UIImageView alloc] init]; 27 } 28 return _imageView; 29 } 30 31 -(UIImage *)image 32 { 33 return self.imageView.image; 34 } 35 36 -(void)setImage:(UIImage *)image 37 { 38 self.imageView.image = image; 39 [self.imageView sizeToFit]; 40 self.scrollView.contentSize = self.image ? self.image.size : CGSizeZero; //设置滚动视图可移动特性 41 } 42 43 -(void)viewDidLoad 44 { 45 [super viewDidLoad]; 46 [self.scrollView addSubview:self.imageView]; 47 }
2、多线程调用方法
1 -(void)setImageURL:(NSURL *)imageURL 2 { 3 _imageURL = imageURL; 4 //self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.imageURL]]; 5 [self startDownloadingImage]; 6 } 7 8 -(void)startDownloadingImage 9 { 10 self.image = nil; 11 if (self.imageURL) { 12 [self.spinner startAnimating]; 13 NSURLRequest *request = [NSURLRequest requestWithURL:self.imageURL];//设置请求对象 14 NSURLSessionConfiguration *congiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration];//设置请求任务配置方法 15 NSURLSession *session = [NSURLSession sessionWithConfiguration:congiguration];//定义URL任务 16 NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request 17 completionHandler:^(NSURL *locationfile, NSURLResponse *response, NSError *error) { 18 if (!error) { 19 if ([request.URL isEqual:self.imageURL]) { 20 UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:locationfile]]; 21 //self.image = image; 22 dispatch_async(dispatch_get_main_queue(), ^{ 23 self.image = image; 24 }); 25 //[self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; 26 } 27 } 28 }];//定义一个线程任务 在子线程中调用网络下载,并把在下载成功的图片在主线程中调用显示 29 [task resume];//恢复任务线程执行,创建任务时默认是挂起状态,需要恢复才能真正执行 30 } 31 }