iPhone:无法在 NSMutableDictionary 中设置从线程接收的对象
我有一个线程(特别是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将
returnDict
作为参数传递给该方法。您正在传递nil
。其他一些想法:
returnDict
。您可以仅使用 dict 作为方法参数。returnImage
。编辑
由于您显然将
returnDict
作为参数传递给该方法,我的另一个猜测是mainViewController
是无
。除此之外,您的代码看起来很实用。You're not passing
returnDict
as the parameter to the method. You're passingnil
.A couple of other thoughts:
returnDict
. You can just usedict
as the method parameter.returnImage
.edit
Since you apparently are passing
returnDict
as the parameter to the method, my other guess would be thatmainViewController
isnil
. Other than that, your code looks functional.