这是一个复杂的问题,我之前曾问过。我正在尝试使用NSURLConnection(我必须使用)下载图像并将其放在iPhone屏幕上。我有一个名为ImageModel的子视图,它有使用NSURLConnection的方法,我在viewController中有另一个应该显示图像的方法。这是代码:
#import "ImageModel.h"
@implementation ImageModel
@synthesize delegater, receivedData, image;
-(void)initer
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.gusto-graphics.com/updates/wp-content/uploads/2011/02/SENSE__This_picture_makes_none_by_Mathan552.jpg"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
NSLog(@"Connection Failed.");
}
}
//There's a few connection methods after this method, which I didn't really change.
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
image = [UIImage imageWithData:receivedData];
[delegater performSelectorOnMainThread:@selector(didFinishDownload:) withObject:image waitUntilDone:NO];
NSLog(@"Success. Received %d bytes of data", [receivedData length]);
[connection release];
[receivedData release];
}
@end
receivedData,delegater和image都在头文件中声明,并且还有一个协议需要调用一个名为didFinishDownload的方法。
我将此代码放在视图控制器中:
- (void)didFinishDownload:(UIImage*)image {
imageView.image = image;
[imageView setNeedsDisplay];
}
- (void)viewDidLoad {
model = [[ImageModel alloc] init];
[model initer];
[super viewDidLoad];
}
模型和imageView都在头文件中声明,我确保将imageView附加到界面构建器中的实际UIImageView,我甚至检查过以确保图片仍然出现在网站上。尽管如此,我所得到的只是一个灰色屏幕。有人可以帮忙吗?
答案 0 :(得分:0)
解决。我忘了在viewDidLoad方法中使用委托对象。