I am making an OpenSource (github) helper class for downloading images asynchronously (I had major trouble with).
However, I have delegate methods set up to alert the delegate that a image has finished downloading. The problem is that the delegate method is not getting called. I am setting the delegate and everything, but I don't have a clue why the problem is occurring.
Please take a look at my code! I have only posted the relevant code.
MKAsyncImageDownloader.h
@protocol MKAsyncImageDownloaderDelegate <NSObject>
@required
- (void)imageShouldFinishDownloading;
@end
@interface MKAsyncImageDownloader : NSObject {
id <MKAsyncImageDownloaderDelegate> delegate;
}
- (id) initWithDelegate:(id <MKAsyncImageDownloaderDelegate>) delegat;
@property (retain, nonatomic) id <MKAsyncImageDownloaderDelegate> delegate;
@end
MKAsyncImageDownloader.m
- (id) initWithDelegate:(id<MKAsyncImageDownloaderDelegate>) delegat {
self = [super init];
if (self) {
delegate = delegat;
}
return self;
}
- (void)imageAtURLHasDownloaded:(NSDictionary *)dict {
[downloadedImageArray addObject:[dict objectForKey:@"image"]];
[[self delegate] imageShouldFinishDownloading];
}
MKOperation.m Subclass of NSOperation. I alloc/init MKAsynImageDownloader to perform the selector only. Code:
- (void)start {
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:self.targetURL]];
if (image) {
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:image, self.targetURL, nil] forKeys:[NSArray arrayWithObjects:@"image", @"url", nil]];
MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] init];
[downloader performSelectorOnMainThread:@selector(imageAtURLHasDownloaded:) withObject:dict waitUntilDone:YES];
[dict release];
[downloader release];
}
[image release];
}
RootViewController.h
MKAsyncImageDownloader *loader;
RootViewController.m Just to show how I am setting the delegate.
loader = [[MKAsyncImageDownloader alloc] initWithDelegate:self];