开发者

Image loading from background

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!=ni开发者_StackOverflow社区l)
                    [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.


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.


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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜