2010-09-21 117 views
0

我有一个小问题的方法,我已经实现了以下方法 打开图像:在调用viewDidLoad中()

- (void)ladeImage { 
id path = @"http://172.23.1.63:8080/RestfulJava/pics"; 
NSURL *url = [NSURL URLWithString:path]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *img = [[UIImage alloc] initWithData:data]; 
UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 
[self.view addSubview:imgView]; 

}

但我怎么能实现在viewDidLoad中这种方法()这个类的方法。 请问有人可以帮我吗?

回答

1

如果这是在您的UIViewController子类实现中,那么只需复制&将ladeImage方法中的代码粘贴到viewDidLoad中(xcode将为您准备方法实现并将它们注释掉,如果您启动VC子类,则需要取消注释)。

它应该是这样的:

-(void)viewDidLoad { 

    [super viewDidLoad]; 

    id path = @"http://172.23.1.63:8080/RestfulJava/pics"; 
    NSURL *url = [NSURL URLWithString:path]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    UIImage *img = [[UIImage alloc] initWithData:data]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 
    [self.view addSubview:imgView]; 

} 

当然,你可以离开的事情,因为他们并调用[self ladeImage];在您的实现viewDidLoad中的。

+0

哦,是的,这是我搜查的方式! – Marco 2010-09-21 06:40:04

+0

太棒了!你可能想考虑接受答案,那么:p – Toastor 2010-09-21 07:29:17