Ted
Ted

Reputation: 3875

experiencing heavy performance hit in my iOS app

It is my first iOS app, and I am trying to figure out what I am doing wrong here. My app would go up and would hang for a few seconds until it is responsive. The app would go over the web and bring images from there when it starts up. It then builds views from the images with added text. The function that builds up the views is quite long, but basically it is fetching the images data from the web for every single object. I use these methods:

NSURL *url = [NSURL URLWithString: 
   @"http://mysite.com/images/best_trip_ever.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];

Since it is happening tens of times when the app starts, I figured this might be one reason for the performance hit.

The views I am creating are made by adding subviews to a view I create. It is happening for every object with the images I fetched from the web.

I also have a table view, for each row I am using the same methods to display a nice row in the table view again from images brought from the web. Something VERY noticeable in the table view is that when I scroll down the cells would get stuck and not move smoothly.

For each object I store the data in an NSData object with encode/decode methods to fetch data and write it back down to the object.

I don't know if it is the bringing of images from the network makes things so slow (which in my opinion shouldn't be THAT slow. It might takes like 7-8 seconds!) Or is it the act of building the views from the images.

I don't mind showing the spinner rotating symbol for each image until it is available, providing the app would wake up as fast as possible.

Any ideas?

Upvotes: 0

Views: 175

Answers (2)

NeverBe
NeverBe

Reputation: 5038

https://github.com/rs/SDWebImage

It's a very useful framework to load/cache images asynchronously

Upvotes: 2

rmaddy
rmaddy

Reputation: 318814

You are accessing data from the Internet on the main thread of your app. That is always bad and it is what is causing the hangs. Have a look at Apple's sample project called LazyTableImages. It solves this exact problem by loading the images in the background.

Upvotes: 2

Related Questions