效果图

@property (weak,nonatomic)IBOutletUILabel *downLabelInfo;

@property (weak,nonatomic)IBOutletUIImageView *imageView;

@end

@implementationViewController

- (void)viewDidLoad

{

[super viewDidLoad];

NSString *url  =@"http://d.hiphotos.baidu.com/image/w%3D1366%3Bcrop%3D0%2C0%2C1366%2C768/sign=6cbcab9dabec8a13141a53e3c135aaec/aa64034f78f0f7369453c3730855b319ebc41316.jpg";

@autoreleasepool {

NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(downloadImage:)object:url];

[thread start];

}

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

}

-(void) downloadImage:(NSString *)url {

// 要把显示的内容放到同步方法前面即可

self->_downLabelInfo.text =@"正在下载图片中...";

// 同步方法 会卡线程直到完成位置

NSData *imageData = [NSDatadataWithContentsOfURL:[NSURLURLWithString:url]];

UIImage *image =[UIImageimageWithData:imageData];

if(image==nil)

{

}

else

{

//self->_downLabelInfo.text = @"正在下载图片中...";所以这条语句写了也没用

[self performSelectorOnMainThread:@selector(updateImageView:)withObject:(image)waitUntilDone:YES];

}

}

-(void) updateImageView:(UIImage *)image

{

self->_imageView.image = image;

self->_imageView.frame=CGRectMake(self.view.frame.size.width/2-100,self.view.frame.size.height/2-100,200, 200);

self->_downLabelInfo.textColor = [UIColorredColor];

self->_downLabelInfo.text =@"图片下载完成";

}

@end

第二种方式使用 NSURLConnection  sendSynchronousRequest 同步方式获取图片内容并显示

将downloadImage方法修改如下

-(void) downloadImage:(NSString *)url

{

self->_downLabelInfo.text =@"正在下载图片中...";

NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:url]];

NSError *error;

// 此处将会造成阻塞

NSData *data   = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:&error];

if(data ==nil)

{

NSLog(@"nil");

}

else

{

UIImage *image =[UIImageimageWithData:data];

self->_imageView.image = image;

self->_downLabelInfo.textColor = [UIColorredColor];

self->_downLabelInfo.text =@"图片下载完成";

}

}

最新文章

  1. 浅谈javascript面向对象
  2. [MongoDB]入门操作
  3. 使用Mod_JK链接Apache和Tomcat
  4. android中的TextView控件
  5. EdasStudio 开发工具用户手册
  6. Dapper连接Oracle
  7. hdu 1175 连连看 DFS
  8. CH Round #53 -【Nescafé 32】杯NOIP模拟赛
  9. java_抽象类应用
  10. IO库 8.2
  11. Eclipse项目崩溃,使用MyEclipse解决
  12. POJ 1094 Sorting It All Out(经典拓扑+邻接矩阵)
  13. PHP获取中英文字符串的首字母
  14. 开始补习JavaScript的第一天
  15. python简单爬虫 使用pandas解析表格,不规则表格
  16. [少数派]如何学习Git
  17. WC2019 题目集
  18. time与datetime模块
  19. 基于ajax 验证表单是否被占用----------------附:10.25日总结
  20. 《Gradle权威指南》--自定义Android Gradle工程

热门文章

  1. Android随
  2. C——整型提升
  3. MIUI5(红米、小米)打开开发者模式
  4. Scala HandBook
  5. 在windows 10中使用pip安装psutil 4.3.0
  6. tomcat apache 实现负载平衡的小demo
  7. Excel表格导入数据
  8. SQLite常用函数
  9. php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
  10. RN中的onChangeText