sagarkothari
sagarkothari

Reputation: 24810

image loading problem in iPhone ? - from webservice

imgBiteSpot.clipsToBounds=YES; 
    NSData *imageData = [[[NSData alloc]init]autorelease];
    imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ObjBitSpot.StrImagePath]];
    if(imageData==0)
    {
        imgBiteSpot.image=[UIImage imageNamed:@"img-not-found.gif"];
    }
    else {
        UIImage *imgs = [[UIImage alloc] initWithData:imageData];
        UIGraphicsBeginImageContext(CGSizeMake(88,88));
        [imgs drawInRect:CGRectMake(0.0, 0.0, 88.0, 88.0)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        imgBiteSpot.image=newImage;
        [imgs release];
    }


I have loaded an image to imgBiteSpot. The image is loaded from the webservice. The problem is that it takes too much time (35 seconds to 1 minute).

If I remove image -> code / load, the reaction time is just 0.2 seconds.

What is the solution to reduce the time for the image loading process?

Thanks in advance for your kind help.

Upvotes: 0

Views: 658

Answers (2)

dimzzy
dimzzy

Reputation: 247

I've written RemoteImage class to load images over the network asynchronously. It also takes care of freeing the memory when necessary. See this post: http://www.dimzzy.com/blog/2009/11/remote-image-for-iphone/

Upvotes: 1

Tim
Tim

Reputation: 60130

You're using NSData's method dataWithContentsOfURL:, which loads the data synchronously from the server, and thus blocks on the thread until the image is received.

What you should do instead is load a blank or "loading" image into your UIImage, then use NSURLConnection to download the image asynchronously and display it when it's done. See the NSURLConnection reference and the URL Loading System doc.

Upvotes: 3

Related Questions