EDIT: solved. I just forgot to use the delegater object in the viewDidLoad method. Check the code and comments to see what I mean.
This is sort of a compound question with one that I had asked before. I am trying to download an image and put it on an iPhone screen using NSURLConnection (which I have to use). I have a subview called ImageModel that has the methods for using NSURLConnection, and I have another method in the viewController that is supposed to display the image. Here’s the code:
#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, and image are all declared in the header file, and there is also a protocol that requires a method called didFinishDownload to be called.
I put this code in the view controller:
- (void)didFinishDownload:(UIImage*)image {
imageView.image = image;
[imageView setNeedsDisplay];
}
- (void)viewDidLoad {
model = [[ImageModel alloc] init];
[model initer];
[super viewDidLoad];
}
Both model and imageView are declared in the header file, I made sure to attach imageView to an actual UIImageView in the interface builder, and I’ve even checked to make sure the picture still comes up on the website. Despite all of this, though, all I am getting is a gray screen. Can anyone help?
solved. I just forgot to use the delegater object in the viewDidLoad method.