zoukankan      html  css  js  c++  java
  • SD_WebImage-03-多线程+下载任务放入非主线程执行

     1 //
     2 //  UIImageView+WebCache.m
     3 //  02-SDWebImage
     4 //
     5 //  Created by mac on 16/4/20.
     6 //  Copyright © 2016年 mac. All rights reserved.
     7 //
     8 
     9 #import "UIImageView+WebCache.h"
    10 
    11 @implementation UIImageView (WebCache)
    12 
    13 /**
    14  *  注意点:有任务应该开启多线程,把下载放入到非主线程,加载UI必须得放入主线程
    15  */
    16 
    17 - (void)setImageWithURL:(NSURL *)url {
    18     
    19 //    NSData *data = [NSData dataWithContentsOfURL:url];
    20 //    
    21 //    UIImage *image = [UIImage imageWithData:data];
    22 //    
    23 //    self.image = image;
    24     
    25     //1。 开启多线程
    26     [self performSelectorInBackground:@selector(downloadImage:) withObject:url];
    27 }
    28 
    29 - (void)downloadImage:(NSURL *)url {
    30     
    31     NSData *data = [NSData dataWithContentsOfURL:url];
    32     
    33     UIImage *image = [UIImage imageWithData:data];
    34     
    35     //2。 所有和UI相关的操作都应该在主线程中执行
    36     [self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    37 }
    38 
    39 @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    软件工程实践总结
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5411886.html
Copyright © 2011-2022 走看看