我正在开发一个应用程序,其中我的桌面视图中显示了很多图像。这些图像必须来自服务器,因此我创建了另一个线程,在该线程中图像得到处理然后在表格视图单元格上进行设置,以使我们的表格视图顺利且正确地滚动。 我的问题是,当我滚动我的表视图的次数,应用程序获得冻结,一段时间后,Xcode的显示下图所示的消息: - 多个NSThreads同时运行导致应用程序冻结
My table view cell code :-
id object = [imageDictFunctionApp objectForKey:[NSString stringWithFormat:@"%d",functionAppButton.tag]];
if(!object){
NSArray *catdictObject=[NSArray arrayWithObjects:[NSNumber numberWithInt:functionAppButton.tag],indexPath,[NSNumber numberWithInt:i],nil];
NSArray *catdictKey=[NSArray arrayWithObjects:@"btn",@"indexPath",@"no",nil];
NSDictionary *catPassdict=[NSDictionary dictionaryWithObjects:catdictObject forKeys:catdictKey];
[NSThread detachNewThreadSelector:@selector(displayingSmallImageForFunctionsApps:) toTarget:self withObject:catPassdict];
}
else
{
if(![object isKindOfClass:[NSNull class]]){
UIImage *img = (UIImage *)object;
[functionAppButton setImage:img forState:UIControlStateNormal];
}
-(void)displayingSmallImageForFunctionsApps:(NSDictionary *)dict
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSIndexPath *path=[dict objectForKey:@"indexPath"];
NSArray *catArray=[self.functionDataDictionary objectForKey:[self.functionsKeysArray objectAtIndex:path.row]];
int vlaue=[[dict objectForKey:@"no"] intValue];
NSDictionary *dict1=[catArray objectAtIndex:vlaue];
NSString *urlString=[dict1 objectForKey:@"artwork_url_large"];
NSURL *url = [NSURL URLWithString:urlString];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
if(image){
[imageDictFunctionApp setObject:image forKey:[NSString stringWithFormat:@"%d",[[dict objectForKey:@"btn"] intValue]]];
NSMutableDictionary *dict2=[NSMutableDictionary dictionaryWithCapacity:4];
[dict2 setObject:image forKey:@"imageValue"];
[dict2 setObject:[dict objectForKey:@"btn"] forKey:@"tag"];
[dict2 setObject:[dict objectForKey:@"indexPath"] forKey:@"indexPath"];
[self performSelectorOnMainThread:@selector(setImageInCellDictCategroryTable:) withObject:dict2 waitUntilDone:NO];
}
else{
[imageDictFunctionApp setObject:[NSNull null] forKey:[NSString stringWithFormat:@"%d",[[dict objectForKey:@"btn"] intValue]]];
}
[pool drain];
}
- (void)setImageInCellDictCategroryTable:(NSDictionary *)dict{
UITableViewCell *myCell = (UITableViewCell *)[categoriesAndFunctionsTableView cellForRowAtIndexPath:[dict objectForKey:@"indexPath"]];
UIButton *myBtn = (CustomUIButton *)[myCell viewWithTag:[[dict objectForKey:@"tag"] intValue]];
if ([dict objectForKey:@"imageValue"]) {
[myBtn setImage:[dict objectForKey:@"imageValue"] forState:UIControlStateNormal];
}
}
我已经张贴了我的所有代码这可能与此错误有关。请任何人建议我如何解决此问题。
在此先感谢!
非常感谢你的答案。我尝试过使用NSOperationQueue,现在应用程序不是没有冻结,但没有图像在我的按钮上。对象:catPassdict];我们可以通过下面的例子来说明如何使用NSInvocationOperation。 \t \t \t \t \t \t \t \t \t \t \t \t [functionOperationQueue addOperation:invOperation]; \t \t \t \t \t \t \t [invOperation release]; functionOperationQueue是我的全局变量。请你看看我做错了什么。 – Gypsa 2012-03-05 05:44:00
@Gypsa:当你得到图像时,你可能需要强制表格视图重新显示。 – JeremyP 2012-03-05 10:36:47
一旦表格滚动,我看到现在显示的SmallImageForFunctionsApps没有被调用。它是当滚动或滚动操作后没有添加到队列中。 – Gypsa 2012-03-06 04:04:39