Hi, I've setup a class as a delegate for NSURLConnection, I
create the NSURLConnection, provide it a delegate (self) but the
delegate methods don't get called (didReceiveData and DidFinishLoading), any idea? Thanks
create the NSURLConnection, provide it a delegate (self) but the
delegate methods don't get called (didReceiveData and DidFinishLoading), any idea? Thanks
Code:
//In my method
for (int i=0;i<[feedArray count];i++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
imgUrl = [NSURL URLWithString:[[mainDelegate.feedArray objectAtIndex:i] objectForKey:@"img"]];
AsyncImageView *async=[[AsyncImageView alloc] init];
[async loadImageFromURL:imgUrl];
[async release];
[pool release];
}
//My class
@implementation AsyncImageView
@synthesize urlImg;
- (id) init
{
if (self = [super init]) {
appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
}
return self;
}
- (void)loadImageFromURL:(NSURL*)url{
if (connection!=nil) { [connection release]; }
if (data!=nil) { [data release]; }
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//I can see in logs some like:
//<NSURLConnection: 0x10d19d0, http://www.myurl.com/images/thumbnails/08_01.jpg>
//TODO error handling, what if connection is nil?
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) {
data = [[NSMutableData alloc] initWithCapacity:2048];
}
[data appendData:incrementalData];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
[connection release];
connection=nil;
UIImage *img=[UIImage imageWithData:data];
.....
[data release];
data=nil;
}
- (void)dealloc {
[connection cancel];
[connection release];
[data release];
[super dealloc];
}