标签:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSURLConnectionDataDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageview;
@property(nonatomic,strong)NSMutableData *recedate;
- (IBAction)Done:(id)sender;
- (IBAction)ShouCang:(id)sender;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
_recedate =[[NSMutableData alloc]init];
[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.
}
- (IBAction)Done:(id)sender
{
NSString *urlAsString=@"http://gaitaobao1.alicdn.com/tfscom/TB10NzeHXXXXXcFXVXXXXXXXXXX.jpg_180x180xz.jpg";
// NSURL *url=[NSURL URLWithString:urlAsString];
// NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url];
// 不可变
// [NSURLConnection connectionWithRequest:urlRequest delegate:self];
NSURL *url=[NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
//可变
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_recedate appendData:data];
NSLog(@"走近");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *image=[UIImage imageWithData:_recedate];
self.imageview.image=image;
}
- (IBAction)ShouCang:(id)sender {
}
@end
第二部分
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (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.
}
- (IBAction)Done:(id)sender {
UIImage *image=nil;
NSString *urlAsString=@"http://gaitaobao1.alicdn.com/tfscom/TB10NzeHXXXXXcFXVXXXXXXXXXX.jpg_180x180xz.jpg";
NSURL *url=[NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url];
NSError *err=nil;
NSData *imagedate=[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&err];
//这个线程休眠5秒
//
[NSThread sleepForTimeInterval:5];
if (err==nil&&imagedate!=nil) {
image=[UIImage imageWithData:imagedate];
}
else if (err!=nil)
{
NSLog(@"error ==%@",err);
}
self.imageview.image=image;
}
- (IBAction)ShouCang:(id)sender {
}
@end
标签:
原文地址:http://www.cnblogs.com/linximu/p/4446849.html