Code, Code, Revolution!
In a previous post I wrote about downloading an image asynchronously. As my work has progressed with my app (which is soon to be released on app store) I realized that the InternetImage class worked very well in the simulator but crashed my application very often on the actual device. The problem with the first version of the class was that it ran by itself, downloading the image and then calling back to it’s delegate when done. The delegate receiver was supposed to release the InternetImage class when it got the message. At first I was very content with the solution and found it pretty neat. However, when images are not downloaded as fast as they are on the simulator with a 24mbit internet connection, there’s a pretty big chance that the user didn’t wait for the image to load before moving to the next view in the application. What happens is:
This happens more often in a scroll view when you often want to load the next page in advance. On top of this major flaw in the design, it’s not very efficient to download an image that isn’t really needed anymore as the user has already navigate away from the view where it was to be shown.
So now I bring you a much better InternetImage class which is thoroughly tested not only on the simulator but also on a the actual device, my beautiful iPhone 3G
.
This is what has been done to fix the flaws:
Other than that the InternetImage class remains the same. I’ve attached the new InternetImage class in a simple sample project. Happy coding!
Download: InternetImage sample
With this blog I try to provide useful tips and solutions for programming .NET, Objective-C and more. My name is Björn Sållarp, and I love writing code.
iPhone development - Downloading an image asynchronously
November 2nd, 2008 at 1:32 am
[...] Development, iPhone NOTE! I’ve updated the InternetImage class, read about it in this post: Downloading an image asynchronously. Revisited!, you will also find the revised code [...]
shouldAutorotateToInterfaceOrientation?
March 14th, 2009 at 2:16 am
[...] with the app and can easily be changed to download images from the internet. Check out my post on downloading images asynchronously. I’ve done my best to add useful comments along with the code but if you have questions [...]
Sasha Aickin
May 1st, 2009 at 6:44 pm
Great post; very informative. I’m wondering if you have any intentions of open sourcing InternetImage. Right now the files all say that they are “all rights reserved”, which means that no one else can use them.
Björn Sållarp
May 2nd, 2009 at 1:57 am
It is open source. It’s unfortunate that I wrote that in all the files previously. Everything on my blog is free to use and modify in any way you seem fit. I havn’t updated all the “old” code samples with a free to use statement, but my intent is that the code posted on my blog is free to use and modify by anyone.
If you appriciate and use the code I would like it if your link back from your site/blog.
Best regards,
Björn
Adeem Basraa
May 5th, 2009 at 1:33 pm
Thax for a neat code
Cedric Vandendriessche
May 23rd, 2009 at 7:36 pm
Nice piece of code! But I have to say there are 2 memory leaks.
1. You allocate InternetImage without releasing previous one.
2. Same as above but with imageConnection.
I also did this with an other class and also there there’s some general memory leak. I contacted apple about it and they told me to test it on 3.0 latest béta but I don’t want to lose my 2.2.1 install. Maybe you could test your code (or the URLCache apple sampleCode)
Thanks,
Cedric
Björn Sållarp
May 24th, 2009 at 6:54 am
Hello Cedric,
1. In the demo app I allocate a single InternetImage, there is no previous instance to release.
2. Same with ImageConnection.
I suspect the memory leaks are in your own implementation.
NIVETHA
August 25th, 2009 at 5:39 am
Really very nice post…… it helped me very much
NIVETHA
August 25th, 2009 at 5:39 am
thank u
Rutger Bevers
April 6th, 2010 at 10:38 pm
I installed the example application on my iPhone.
It works most of the times, but sometimes the image is never loaded although I have a perfect connection.
You can test this by setting the iPhone into airplane mode and then turn it of. Now immediately when you get connection to the network start the application. The image will never load…
coder
May 19th, 2010 at 11:23 am
yes there is memory leak about InternetImage.
i just put a UIbutton in view.
i pasted your code buttons event:
[self downloadImageFromInternet:@"http://www.google.se/intl/en_com/images/logo_plain.png"];
there is no memory leak when you first tap the button.
but when you tap again
there are memory leaks:
InternetImage
UIImage
i also changed your connection code:
NSURL *url = [NSURL URLWithString:ImageUrl];
NSURLRequest *imageRequest = [[NSURLRequest alloc] initWithURL: url];
imageConnection = [[[NSURLConnection alloc] initWithRequest:imageRequest delegate:self] autorelease];
how can i solve it? i want to refresh the UIImage when the user taps button…
thanx…
Richard Fliam
June 21st, 2010 at 7:11 am
Old post to comment on but the leak occurs when the image reaches a retain count of zero and dealloc is called… before abortDownload or connectionDidFinishLoading: is called. This causes a general leak of downLoadedImage and m_requestData. Adding [self abortDownload]; to the dealloc method should fix this.