委托方法未被调用

发布于 2024-11-03 14:22:56 字数 1931 浏览 2 评论 0原文

我正在制作一个开源(github)帮助程序类,用于异步下载图像(我遇到了很大的麻烦)。

但是,我设置了委托方法来提醒委托图像已完成下载。问题是委托方法没有被调用。我正在设置委托和所有内容,但我不知道为什么会出现问题。

请看一下我的代码!我只发布了相关代码。

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 NSOperation 的子类。 我分配/初始化 MKAsynImageDownloader 仅执行选择器。 代码:

- (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 只是为了展示我如何设置代表。

 loader = [[MKAsyncImageDownloader alloc] initWithDelegate:self];

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2024-11-10 14:22:56

在您的 start 方法中,您永远不会调用正确的 init 方法,您正在调用:

MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] init];

您应该调用

MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] initWithDelegate:myDelegate];

然后您说您在其他地方设置了 loader 对象?这是两个独立的对象,您实际使用的对象就是上面引用的对象。

那就是:

loader = [[MKAsyncImageDownloader alloc] initWithDelegate:self];

不做功

MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] init];

。您的 MKOperation 没有对具有委托集的 loader 类的引用。

你说:

MKOperation.m NSOperation 的子类。我分配/初始化 MKAsynImageDownloader 仅执行选择器。

我认为你误解了你在这里所做的事情。您将创建一个全新的 MKAsynImageDownloader 实例,并在该实例上执行选择器,而不是在 RootController 中的 loader 实例。您可能希望让 MKOperation 在初始化期间获取 MKAsyncImageDownloader 对象。

编辑:

这是我指的“下载器”。在您的 MKOperation 的 start 中,

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];
 }

这是与 RootViewController 中的实例不同的实例,该实例没有委托集。这是您正在处理的工作,因此,这是尝试通知委托人的工作...但同样,它没有有委托集。

In your start method you are never calling your proper init method, you are calling:

MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] init];

you should be calling

MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] initWithDelegate:myDelegate];

Then you say you set a loader object somewhere else? These are two separate objects, the one you actually seem to use is what is referenced above.

That is:

loader = [[MKAsyncImageDownloader alloc] initWithDelegate:self];

does not make

MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] init];

work. Your MKOperation has no reference to your loader class that has the delegate set.

You state:

MKOperation.m Subclass of NSOperation. I alloc/init MKAsynImageDownloader to perform the selector only.

I think you misunderstand what you are doing here. You are create a brand new instance of MKAsynImageDownloader and performing the selector on that instance, not the loader instance that lives in your RootController. You probably want to have MKOperation take a MKAsyncImageDownloader object during it init.

EDIT:

This is the "downloader" I'm referring to. In your MKOperation's start

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];
 }

That is the separate instance from the one that lives in your RootViewController, this one does not have a delegate set. THIS is the one you are doing work on, therefore, this one is the one attempting to notify the delegate... but again, it doesn't have a delegate set.

夏の忆 2024-11-10 14:22:56

您研究过 SDWebImage 吗?

有时,解决问题的最简单方法是使用无需维护的工作代码...

https:// github.com/rs/SDWebImage

Have you looked into SDWebImage?

Sometimes the easiest way to solve a problem is to use working code you don't have to maintain...

https://github.com/rs/SDWebImage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文