iPhone:无法在 NSMutableDictionary 中设置从线程接收的对象

发布于 2024-09-16 01:56:16 字数 1570 浏览 5 评论 0原文

我有一个线程(特别是 NSOperation),当我的主视图需要它们时,它会运行为我加载一些图像以用于滚动视图。可以同时对任意数量的这些 NSOperations 进行排队。因此它与我给它的文件路径一致,并从磁盘加载图像(作为 UIImages),然后使用 PerformSelectorOnMainThread: 将对象发送回我的主视图,并向我的主视图传递对象的 NSDictionary 和图像 ID 值。然后,我的主视图应该将图像对象和图像 ID 字符串插入到 NSMutableDictionary 中,供主视图使用。我已经验证 NSMutableDictionary 已正确分配和初始化,但是当 NSOperation 调用的方法尝试将对象添加到字典中时,没有任何反应。我已经验证从线程发送给我的字典中获得的对象和字符串不为空或任何内容,但它不起作用。我是否做错了什么或使用了错误的技术?在这种情况下,我需要从线程将 UIImages 添加到 NSMutableDictionary,有人会建议做什么?非常感谢!

这是我使用的 NSOperation 代码:

- (void)main {
    NSString *filePath = [applicaitonAPI getFilePathForCachedImageWithID:imageID andSize:imageSize];
    UIImage *returnImage = [[UIImage alloc] initWithContentsOfFile:filePath];

    if (returnImage) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
        [dict setObject:returnImage forKey:@"IMAGE"];
        [dict setValue:[NSString stringWithFormat:@"%d", imageID] forKey:@"IMAGE_ID"];
        NSDictionary *returnDict = [[NSDictionary alloc] initWithDictionary:dict];
        [dict release];
        [mainViewController performSelectorOnMainThread:@selector(imageLoaderLoadedImage:) withObject:returnDict waitUntilDone:NO];
        [returnDict release];
    }
}

这是主线程上的方法:

- (void)imageLoaderLoadedImage:(NSDictionary *)dict {
  UIImage *loadedImage = [dict objectForKey:@"IMAGE"];
  NSString *loadedImage = [dict valueForKey:@"IMAGE_ID"];
  [imagesInMemoryDictionary setObject:loadedImage forKey:loadedImageID];
  [self drawItemsToScrollView];
}

I've got a thread (specifically an NSOperation) that runs to load some images for me for a scroll view when my main view asks for them. Any number of these NSOperations can be queued at once. So it goes with the filepath I give it and loads the image from the disk (as UIImages) and then sends the object back to my mainview by using performSelectorOnMainThread: and passing my mainview an NSDictionary of the object, and an image ID value. My main view is then supposed to insert the image object and the image ID string into an NSMutableDictionary that it has for the mainview to be able to use. I've verified that the NSMutableDictionary is allocated and initialized fine, but when the method the NSOperation calls tries to add the objects to the dictionary nothing happens. I've verified that the object and string i get from the dictionary the thread sent me are not null or anything but yet it doesn't work. Am I not doing something right or using a bad technique? What would anyone suggest to do in a situation like this where I need to add UIImages to an NSMutableDictionary from a thread? Thanks so much!

Here's the NSOperation code I use:

- (void)main {
    NSString *filePath = [applicaitonAPI getFilePathForCachedImageWithID:imageID andSize:imageSize];
    UIImage *returnImage = [[UIImage alloc] initWithContentsOfFile:filePath];

    if (returnImage) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
        [dict setObject:returnImage forKey:@"IMAGE"];
        [dict setValue:[NSString stringWithFormat:@"%d", imageID] forKey:@"IMAGE_ID"];
        NSDictionary *returnDict = [[NSDictionary alloc] initWithDictionary:dict];
        [dict release];
        [mainViewController performSelectorOnMainThread:@selector(imageLoaderLoadedImage:) withObject:returnDict waitUntilDone:NO];
        [returnDict release];
    }
}

And here's the method on the main thread:

- (void)imageLoaderLoadedImage:(NSDictionary *)dict {
  UIImage *loadedImage = [dict objectForKey:@"IMAGE"];
  NSString *loadedImage = [dict valueForKey:@"IMAGE_ID"];
  [imagesInMemoryDictionary setObject:loadedImage forKey:loadedImageID];
  [self drawItemsToScrollView];
}

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

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

发布评论

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

评论(1

甚是思念 2024-09-23 01:56:16
[mainViewController performSelectorOnMainThread:@selector(imageLoaderLoadedImage:) withObject:nil waitUntilDone:NO];

您没有将 returnDict 作为参数传递给该方法。您正在传递nil

其他一些想法:

  • 您不需要创建 returnDict。您可以仅使用 dict 作为方法参数。
  • 你正在泄漏returnImage

编辑

由于您显然returnDict作为参数传递给该方法,我的另一个猜测是mainViewController。除此之外,您的代码看起来很实用。

[mainViewController performSelectorOnMainThread:@selector(imageLoaderLoadedImage:) withObject:nil waitUntilDone:NO];

You're not passing returnDict as the parameter to the method. You're passing nil.

A couple of other thoughts:

  • you don't need to create returnDict. You can just use dict as the method parameter.
  • you're leaking returnImage.

edit

Since you apparently are passing returnDict as the parameter to the method, my other guess would be that mainViewController is nil. Other than that, your code looks functional.

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