从背景加载图像
我正在后台加载图像。使用此代码
-(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,但它不会在滚动表格后加载图像显示。
我想在这些图像加载后立即显示它们。
请帮我 。提前致谢。
I am loading images in background . using this code
-(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];
}
}
Here i am using reloadData inside the method after each image load but it does not load images shows after scrolling the table.
I want to show these images as soon as they load.
Please help me . Thanx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ishu,
尝试使用这个代替
[mTable reloadData]
:顺便说一下,很抱歉,但是这段代码完全不可读。
Ishu,
Try to use this instead of
[mTable reloadData]
:By the way, sorry to say that, but this code is completely unreadable.
Ishu,您的代码的基本问题是您无法在辅助线程中通过 UIImage imageWithData API 加载图像。 UIKit APIS 只能在主线程中使用。您可以做的是下载项目并将它们保存到辅助线程中的文件中,但您仍然需要在主线程中加载该图像数据。如果您只想下载然后加载图像数据,您甚至不需要辅助线程,因为 NSURLConnection 类已经支持异步完成回调,下载会在后台线程中自动完成。
Ishu, the basic problem with your code is that you can't load images via the UIImage imageWithData API in a secondary thread. UIKit APIS can only be used from the main thread. What you can do is download items and save them to files in a secondary thread, but you still need to load that image data in the main thread. If you just want to download and then load the image data, you don't even need a secondary thread as the NSURLConnection class already has support for an async done callback, the download is automatically done in a background thread.
在代码中添加此行
[m_Table reloadData];
我使用此行add this line in code insted of
[m_Table reloadData];
i use this line