从背景加载图片
我在后台加载图片。使用此代码从背景加载图片
-(void)backgroundImageLoading
{
if(isGroupOrFriend)
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
for(NSDictionary *d in m_kwikiArray)
{
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
if([d valueForKey:@"UserImage"]!=nil && ![[d valueForKey:@"UserImage"] isEqualToString:@""])
{
UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[d valueForKey:@"UserImage"]]]];
if(img!=nil)
[dict setObject:img forKey:@"UserImage"];
}
else
[dict setObject:@"No Image" forKey:@"UserImage"];
//if([d valueForKey:@"imagePath"]!=nil && ![[d valueForKey:@"imagePath"] isEqualToString:@""])
// [dict setObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[d valueForKey:@"imagePath"]]]] forKey:@"imagePath"];
// else
// [dict setObject:@"NO Image" forKey:@"imagePath"];
if([[d valueForKey:@"Transactions"] count]>0)
{
if([[[d valueForKey:@"Transactions"] objectAtIndex:0] valueForKey:@"UserThumbnail"]!= nil && ![[[[d valueForKey:@"Transactions"] objectAtIndex:0]valueForKey:@"UserThumbnail"] isEqualToString:@""])
{
UIImage *img1=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[d valueForKey:@"Transactions"] objectAtIndex:0] valueForKey:@"UserThumbnail"]]]];
if(img1!=nil)
[dict setObject:img1 forKey:@"UserThumbnail1"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail1"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail1"];
if([[d valueForKey:@"Transactions"] count]>1)
{
if([[[d valueForKey:@"Transactions"] objectAtIndex:1] valueForKey:@"UserThumbnail"]!=nil && ![[[[d valueForKey:@"Transactions"] objectAtIndex:1] valueForKey:@"UserThumbnail"] isEqualToString:@""])
{
UIImage *img2=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[d valueForKey:@"Transactions"] objectAtIndex:1] valueForKey:@"UserThumbnail"]]]];
if(img2!=nil)
[dict setObject:img2 forKey:@"UserThumbnail2"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail2"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail2"];
[imageForGruopAndFriendArray addObject:dict];
}
//isReloadTable=YES;
[mTable reloadData];
[pool release];
}
else
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
for(NSDictionary *d in m_kwikiArray)
{
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
if([d valueForKey:@"Illustration"]!=nil && ![[d valueForKey:@"Illustration"] isEqualToString:@""])
{
UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[d valueForKey:@"Illustration"]]]];
if(img!=nil)
[dict setObject:img forKey:@"Illustration"];
}
else
[dict setObject:@"No Image" forKey:@"Illustration"];
if([d valueForKey:@"imagePath"]!=nil && ![[d valueForKey:@"imagePath"] isEqualToString:@""])
{
UIImage *img1=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[d valueForKey:@"imagePath"]]]];
if(img1!=nil)
[dict setObject:img1 forKey:@"imagePath"];
}
else
[dict setObject:@"NO Image" forKey:@"imagePath"];
if([[d valueForKey:@"Transactions"] count]>0)
{
if([[[d valueForKey:@"Transactions"] objectAtIndex:0] valueForKey:@"UserThumbnail"]!= nil && ![[[[d valueForKey:@"Transactions"] objectAtIndex:0]valueForKey:@"UserThumbnail"] isEqualToString:@""])
{
UIImage *img2=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[d valueForKey:@"Transactions"] objectAtIndex:0] valueForKey:@"UserThumbnail"]]]];
if(img2!=nil)
[dict setObject:img2 forKey:@"UserThumbnail1"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail1"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail1"];
if([[d valueForKey:@"Transactions"] count]>1)
{
if([[[d valueForKey:@"Transactions"] objectAtIndex:1] valueForKey:@"UserThumbnail"]!=nil && ![[[[d valueForKey:@"Transactions"] objectAtIndex:1] valueForKey:@"UserThumbnail"] isEqualToString:@""])
{
UIImage *img3=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[d valueForKey:@"Transactions"] objectAtIndex:1] valueForKey:@"UserThumbnail"]]]];
if(img3!=nil)
[dict setObject:img3 forKey:@"UserThumbnail2"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail2"];
}
else
[dict setObject:@"NOImage" forKey:@"UserThumbnail2"];
[imageArray addObject:dict];
}
//isReloadTable=YES;
[mTable reloadData];
[pool release];
}
}
这里我使用reloadData每个图片加载后的方法,但它不会加载图片滚动表后显示。
我想在加载后立即显示这些图像。
请帮帮我。提前Thanx。
在代码中加入这一行insted的的[m_Table reloadData];
我用这条线
[m_Table performSelectorOnMainThread:@selector(reloadData) withObject:self waitUntilDone:NO];
Ishu,
尝试使用它代替的[mTable reloadData]
:
[mTable beginUpdates];
[mTable endUpdates];
顺便说一句,不好意思说,但是这个代码是完全不可读。
这不工作仍然需要滚动显示在表中的图像。 – Ishu 2011-06-09 08:01:34
我完全同意Zapko - 你的代码是不可读的。 – 2011-06-09 08:09:49
Ishu,您的代码的基本问题是,您无法通过UIImage imageWithData API在辅助线程中加载图像。 UIKit APIS只能在主线程中使用。你可以做的是下载项目并将它们保存到辅助线程中的文件中,但是仍然需要在主线程中加载该图像数据。如果您只是想下载并加载图像数据,您甚至不需要辅助线程,因为NSURLConnection类已经支持异步完成回调,下载会自动在后台线程中完成。
你说得对。我已经解决了我的问题,看到我的答案。 – Ishu 2011-06-10 04:51:46
你们是不是比主线程之外的其他线程更新UI? – 2011-06-09 07:37:39