IOS多线程下载图片
2016-04-18 19:35
399 查看
ViewController.h
上面定义一个imageView
ViewController.m
点击骚紫色button显示图片
mac电脑不太会操作就不截图了
#import <UIKit/UIKit.h> @interface ViewController : UIViewController{ UIImageView *imageView; } @property(nonatomic, retain) IBOutlet UIImageView *imageView; @end
上面定义一个imageView
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize imageView; - (void)viewDidLoad { [super viewDidLoad]; NSString *url = @"http://d.hiphotos.baidu.com/image/w%3D1366%3Bcrop%3D0%2C0%2C1366%2C768/sign=6cbcab9dabec8a13141a53e3c135aaec/aa64034f78f0f7369453c3730855b319ebc41316.jpg" ; // Do any additional setup after loading the view, typically from a nib. UIButton *myDemoButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 200, 50)]; [myDemoButton setBackgroundColor:[UIColor purpleColor]]; [myDemoButton addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:myDemoButton]; // [self downLoadImage:[NSString stringWithFormat:@"%s", IMAGE_VIEW]]; NSThread *myThread = [[NSThread alloc] initWithTarget:self selector:@selector(downLoadImage:) object:url]; [myThread start]; } - (void)myButtonPressed:(id)sender{ NSLog(@"myDemoButton pressed"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)downLoadImage:(NSString *)url{ NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url]]; sleep(5); UIImage *imageImage = [[UIImage alloc] initWithData:imageData]; if(imageImage){ [self performSelectorOnMainThread:@selector(updateUI:) withObject:imageImage waitUntilDone:YES]; } } - (void)updateUI:(UIImage *)image{ self.imageView.image = image; } @end
点击骚紫色button显示图片
mac电脑不太会操作就不截图了
相关文章推荐
- [ios][opengles]opengles在ios上的透明问题
- 【笔记】iOS开发基础笔记<四>
- 【iOS程序启动与运转】- RunLoop个人小结
- iOS开发-Xcode debug,release的区别
- Masonry框架的基本使用
- ios申请真机调试( xcode 5)详细解析
- iOS非CocoPods集成七牛播放SDK PLPlayerKit 使用指南
- iOS开发---点击查看缩略图
- Jack--10天学会IOS大纲;注意将图片放大观看!
- 用OCLint给iOS代码做静态分析
- iOS的layer的anchorpoint与posion问题
- iOS学习笔记35-社交分享
- iOS学习笔记35-社交分享
- iOS从项目中删除Cocoapods
- iOS开发判断版本号不同
- iOS大牛博客集
- Windows上使用ionic开发IOS程序
- 从零迈向iOS--新手篇
- iOS之Cookie
- iOS使用AVFoundation实现二维码扫描(ios7以上)——转载