Network Data Synchronization request loading and Data Synchronization request Loading

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.