NSData类方法的同步请求、NSString类方法的同步请求

#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(@"下载完毕");
}


-(void)synDownloadWithString1:(NSURL *)url{
    NSError *error;
    NSString *str = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    
    if (error) {
        NSLog(@"%@",error);
    }
    
    
}




#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.
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值