Network Data Synchronization request loading and Data Synchronization request Loading
# Import "ViewController. h"
@ Interface ViewController ()
@ Property (nonatomic, weak) IBOutlet UIButton * button;
@ Property (nonatomic, weak) IBOutlet UIImageView * imageView;
@ End
@ Implementation ViewController
-(IBAction) btnClick :( id) sender
{
// Give a url string
NSString * str = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 3 ";
NSString * str2 = @ "http://photo.candou.com/ I /114/826ea823e8ffe792a6fda9e126f6c404 ";
// Encapsulate it into available URL types;
NSURL * url = [NSURL URLWithString: str];
NSURL * url2 = [NSURL URLWithString: str2];
[Self syncDownloadWithString: url];
[Self syncDownloadWithData: url2];
}
# Synchronous request of The pragma mark-NSDate class method-
-(Void) syncDownloadWithData :( NSURL *) url {
// Synchronous request method provided by binary data
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 (@ "download completed ");
}
# Pragma mark-NSString synchronous request-
-(Void) syncDownloadWithString :( NSURL *) url {
// Synchronous request method provided by string
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