iOS 네트워크 NSURL 2

2388 단어
#import <UIKit/UIKit.h>

@interface MyImageView : UIImageView<NSURLConnectionDataDelegate>
@property (retain, nonatomic) NSMutableData *data;
-(void)setMyImageViewUrl:(NSURL *)url;
@end
#import "MyImageView.h"

@implementation MyImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

-(void)setMyImageViewUrl:(NSURL *)url
{
    self.data=[NSMutableData data];
    
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
    [request setHTTPMethod:@"GET"];
    [request setURL:url];
    [request setTimeoutInterval:60];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}
#pragma mark---NSUrlConnectionData  delegate
// 
// 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.data  appendData:data];
}
// 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    UIImage *image=[UIImage imageWithData:self.data];
    self.image=image;
}

@end
#import "MyAppDelegate.h"
#import "UIImageView+catchImage.h"
#import "MyImageView.h"
@implementation MyAppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    
//    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 20, 200, 120)];
//    [imageView setImageUrl:[NSURL URLWithString:@"http://news.imust.cn/UploadFiles/2013/2/2013112015485327367.jpg"]];
//    [self.window addSubview:imageView];
    
    MyImageView *imageView=[[MyImageView alloc]initWithFrame:CGRectMake(60, 20, 200, 120)];
    [imageView setMyImageViewUrl:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=26a4246642a7d933aba5ec21cc22ba76/5ab5c9ea15ce36d3fc2e9d423af33a87e850352ac65c99a5.jpg"]];
    [self.window addSubview:imageView];
  
    
    return YES;
}

좋은 웹페이지 즐겨찾기