美文网首页iOS 多线程
iOS--多线程_GCD2 - 封装方法网络加载图片

iOS--多线程_GCD2 - 封装方法网络加载图片

作者: STONEsh | 来源:发表于2016-02-28 15:38 被阅读261次

    ViewController.m#

    //
    //  ViewController.m
    //  多线程_GCD2
    
    #import "ViewController.h"
    #import "UIImageView+WebCatc.h"
    @interface ViewController ()
    
    @property(nonatomic, strong)UIImageView *imageV;
    
    @property(nonatomic, strong)UIScrollView *scrollV;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
     //  // //
        _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 414, 500)];
        [self.view addSubview:_imageV];
    
     // // //
        
       
        
        
        
        
    }
    
    
    - (IBAction)downLoadImageAction:(UIButton *)sender {
        
        NSLog(@"加载图片");
        
        NSString *str = @"http://v1.qzone.cc/pic/201512/26/15/07/567e3ca50c589469.jpg!600x600.jpg";
        
        [_imageV setImageWithURL:[NSURL URLWithString:str]];
        
    
        
    }
    
    
    - (IBAction)load:(id)sender {
        
        NSLog(@"加载轮播图");
         self.scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake (0,100,414,500)];
        
        _scrollV.contentSize = CGSizeMake(414*5,500);
        for (int i = 0; i <5; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(414*i, 0, 414, 500)];
            
            NSString *urlString = @"http://v1.qzone.cc/pic/201512/26/15/07/567e3ca50c589469.jpg!600x600.jpg";
            [imageView setImageWithURL:[NSURL URLWithString:urlString]];
            [self.scrollV addSubview:imageView];
        }
        
        
        
        
        [self.view addSubview:self.scrollV];
        
    
    
    }
    
    - (IBAction)lianxutupian:(UIButton *)sender {
        
        NSLog(@"加载连续图片");
        
        // // // //
        for (int i = 0; i < 10; i++) {
    
    
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200*i+50, 394, 200)];
    
            [self.view addSubview:imageV];
    
            NSString *urlString = @"http://v1.qzone.cc/pic/201512/26/15/07/567e3ca50c589469.jpg!600x600.jpg";
    
            [imageV setImageWithURL:[NSURL URLWithString:urlString]];
    
        }
    
        
        
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    

    UIImageView+WebCatc.h#

    //
    //  UIImageView+WebCatch.h
    // 多线程_GCD2
    
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIImageView (WebCatch)
    
    -(void)setImageWithURL:(NSURL *)url;
    
    @end
    
    

    UIImageView+WebCatc.m#

    //
    //  UIImageView+WebCatch.m
    //  多线程_GCD2
    //
    
    
    #import "UIImageView+WebCatc.h"
    
    @implementation UIImageView (WebCatch)
    
    //法一
    -(void)setImageWithURL:(NSURL *)url{
    
        dispatch_queue_t queue = dispatch_queue_create("loadImage", NULL);
        dispatch_async(queue, ^{
            
            NSData *data = [NSData dataWithContentsOfURL:url];
            
            UIImage *image = [UIImage imageWithData:data];
            
            
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.image = image;
            });
        });
        
        
    
        
    }
    
    //法二
    //-(void)setImageWithURL:(NSURL *)url{
    //
    //   [self performSelectorOnMainThread:@selector(loadData:) withObject:url waitUntilDone:YES];
    //
    //}
    //
    //
    //-(void)loadData:(NSURL *)url{
    //    
    //    @autoreleasepool {
    //        
    //        NSData *data = [NSData dataWithContentsOfURL:url];
    //        
    //        UIImage *image = [UIImage imageWithData:data];
    //        
    //        //在主线程上执行setImage方法
    //        
    //        [self performSelectorOnMainThread:@selector(setImages:) withObject:image waitUntilDone:YES];
    //        
    //    }
    //    
    //}
    
    @end
    
    

    相关文章

      网友评论

        本文标题:iOS--多线程_GCD2 - 封装方法网络加载图片

        本文链接:https://www.haomeiwen.com/subject/trijkttx.html