Search code examples
iphoneiphone-sdk-3.0ios4uiimage

Loading Image into UIImageView using NSURLConnection


Hey all. I'm really new at this obj-c/xcode stuff. I'm trying to load the background of my xib file. To do this i'm using a UIImageView and populating that with an image I found from the net. the problem with that is that it's REALLY slow. Feels like it's crashing but it's not. I was told to use an NSURLConnection to fix the problem but don't know how. Here's the code i was using previously.

wallpaper.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://mysite.com/mobile/wallpaperR.asp?id=%i",customerID]]]];

How do i translate the above code into the NSURLConnection equivalent?


Solution

  • NSURLConnection will download the data in a new thread, so you app will feel much faster.

    It is pretty easy to implement but it does require you to understand the concept of delegation.

    I have a useful class I created to handle image downloads on separate threads, I will share the code with you, with comments to let you know what is going on:

    AsyncImage.h

    AsyncImage.m

    If you need help implementing it just leave a comment and I can help you get it working, I remember this used to be pain for me too when I started developing.