NSString类方法同步请求、使用NSData方法同步请求 下载图片、数据
2015-08-30 00:08
465 查看
#import "ViewController.h" @interface ViewController () @property(nonatomic,weak)IBOutlet UIButton *button; @property(nonatomic,weak)IBOutlet UIImageView *imageView; @end @implementation ViewController -(IBAction)btnClick:(id)sender { //给一个url字符串 NSString *str = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=3"; NSString *str2 = @"http://photo.candou.com/i/114/826ea823e8ffe792a6fda9e126f6c404"; //封装成可用的URL类型; NSURL *url = [NSURL URLWithString:str]; NSURL *url2 = [NSURL URLWithString:str2]; [self syncDownloadWithString:url]; [self syncDownloadWithData:url2]; } #pragma mark -NSData类方法的同步请求- -(void)syncDownloadWithData:(NSURL *)url{ //二进制数据提供的同步请求方法 NSData *data = [NSData dataWithContentsOfURL:url]; //NSData -->UIImage UIImage *image = [UIImage imageWithData:data]; //UIImage -->NSData NSData *dataImage = UIImagePNGRepresentation(image); NSData *dataImage2 = UIImageJPEGRepresentation(image, 0.5); UIImage *image3 = [UIImage imageWithContentsOfFile:@""]; self.imageView.image = image; NSLog(@"下载完毕"); } #pragma mark -NSString类方法的同步请求- -(void)syncDownloadWithString:(NSURL *)url{ //字符串提供的同步请求的方法 NSError *error; NSString *str = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (error) { NSLog(@"%@",error); return; } //NSString --->NSData; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; //NSDate -->NSString NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",strData); NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",dict); NSLog(@"str:%@",str); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
相关文章推荐
- 【Tomcat】tomcat简单介绍,安装以及启动(一)
- ambari领域对象关系图
- word生成目录
- Cocos2d-x坐标系理解
- Dede织梦SEO优化及设置教程
- 信息系统运维资质与ITSS运维标准浅析
- iOS9允许广告拦截 苹果或为推广自家新闻客户端
- 存储过程示例以及jdbc调用存储过程
- java实现快速排序
- 剪刀、石头、布
- 制作公司内部私有源
- 在IntelliJ IDEA中添加框架支持时找不到Hibernate的解决办法
- 星际迷航
- libmad编译
- 基于SVG的票面设计器开发总结
- 机器学习中如何选择分类器
- 惊闻Yahoo Pipes将关闭 有好的RSS聚合工具推荐吗
- 通过实例浅析Python对比C语言的编程思想差异
- php-fpm设置socket方式连接FastCGI
- 简单谈谈javascript中的变量、作用域和内存问题