多线程加载图片demo

来源:互联网 发布:杜蕾斯凸点螺纹 知乎 编辑:程序博客网 时间:2024/04/29 19:41

最近我在做一个电子商务平台(只是为了电商大赛),商品的小图片需要异步加载。于是,我就参考网上的代码,写了一个demo。

#import "ViewController.h"  #define kURL @"http://yourservername.csdn.net/2/C/D/1_totogo2010.jpg"  @interface ViewController ()  @end  @implementation ViewController  -(void)downloadImage:(NSString *) url{      NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];      UIImage *image = [[UIImage alloc]initWithData:data];      if(image == nil){      }else{          [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];      }  }  -(void)updateUI:(UIImage*) image{      self.imageView.image = image;  }  - (void)viewDidLoad  {      [super viewDidLoad];  //    [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];      NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];      [thread start];  }  - (void)didReceiveMemoryWarning  {      [super didReceiveMemoryWarning];      // Dispose of any resources that can be recreated.  }  @end  
0 0
原创粉丝点击