您的位置:首页 > 移动开发 > IOS开发

NSThread简单实用

2015-10-13 09:29 495 查看
  
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

NSThread * thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"http://img4.duitang.com/uploads/item/201312/22/20131222103125_zLmFL.jpeg"];
[thread start];

}
-(void)run:(NSString* )url{

NSData*data= [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url]];

UIImage* img=[[UIImage alloc]initWithData:data];

if (img!=nil) {
//到主线程去更新UI
[self performSelectorOnMainThread:@selector(update:) withObject:img waitUntilDone:YES];
}
}

-(void)update:(UIImage*)img{
[_imgView setImage:img];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios