·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 网络数据同步请求加载

网络数据同步请求加载

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

#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 -NSDate类方法的同步请求-

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

@end