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