从背景加载图像

发布于 2024-11-15 00:15:53 字数 5993 浏览 1 评论 0原文

我正在后台加载图像。使用此代码

-(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 技术交流群。

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

发布评论

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

评论(3

明月夜 2024-11-22 00:15:53

Ishu,

尝试使用这个代替 [mTable reloadData]

[mTable beginUpdates];
[mTable endUpdates];

顺便说一下,很抱歉,但是这段代码完全不可读。

Ishu,

Try to use this instead of [mTable reloadData]:

[mTable beginUpdates];
[mTable endUpdates];

By the way, sorry to say that, but this code is completely unreadable.

和我恋爱吧 2024-11-22 00:15:53

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.

吖咩 2024-11-22 00:15:53

在代码中添加此行 [m_Table reloadData]; 我使用此行

[m_Table performSelectorOnMainThread:@selector(reloadData) withObject:self waitUntilDone:NO];

add this line in code insted of [m_Table reloadData]; i use this line

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