Saving images in Document Directory - iphone

I am downloading images from web in uitableview and want to save in document directory. I am downloading syncronously. I downloaded and saved in document directory successfully, but when i scroll uitable every time images are being saved , i want to save images at the time of downloading only once. What should i do? Can anybody provide me some code? Any help will be appreciated.

You should never use synchronous download. It will block your UI, make your UITableView scrolling lag, make the user think your app has frozen... Very bad idea.
You should instead always use asynchronous download using NSURLConnection's loadRequest: method and implementing the delegate methods yourself (even probably dedicate a class to manage the download, as everybody do)
See the LazyTableImages sample code from Apple Documentation, it explains how to load images asychronously to display them in a TableView, loading them on demand, and avoiding to freeze the UI.
Never do blocking operations in the delegate methods of UITableView like tableView:cellForRowAtIndexPath: (and I hope you use the reuse-mechanism of UITableViewCells correctly too, to reduce cell allocations. See the TableView Programming Guide about this point)
Once you have implemented the asynchronous download using NSURLConnection and its delegate methods, its a piece of cake to move the code that saves the images in the documents folder in the connectionDidFinishLoading: delegate method (thus saving them only once and only when they are downloaded)

Do the saving of images in didConnectionFinishLoading method. You can also use an NSNotification for this, to know when the download is getting completed.
Hope this information is useful.

Related

ios load images from web server and show on UITableViewCell

For the iPhone 5.0 (ARC) I am getting lot of image(image url's) and text data from server in a json form. My requirement is to load the first five images from the server and when the user scrolls down the next five images load and so on. I need to show it in a UITableViewCell. By doing this I can reduce the network calls and make the application faster on the device.
Currently, I am using a background thread to load images, but they continuously load in the background. I don't want to do it like this.
I strongly encourage you to use the AFNetworking framework (that is really great and complete for all network-related tasks).
It comes with a category on UIImageView that allows you to set the UIImageView's image directly from an NSURL, managing everything for you in the background, like downloading the image or fetching it from its cache, cancelling the request if you change the URL later to avoid useless requests, etc.
Then in your tableView:cellForRowAtIndexPath: you can simply write this kind of code:
[cell.imageView setImageWithURL:yourURL];
And you're done. It will work even when the recycling mechanism of the UITableView is in action when you scroll, which would be quite a pain to manage using other methods.
Take a look at SDWebImage. It's a really neat library to download images asynchronously and also caches them.

iPhone : UITableView reloadData timing problem

I'm encountering a common TableView data reloading problem. I've read many questions on the same subject but the problem was never exactly the same as mine...
I have a navigation based application. In the RootViewController's viewDidLoad method I make a request in order to get JSON data (articles). When the connection has finished loading I create custom Article objects for each entry. The Article class has an initWithDictionnary method which initializes the attributes of the object and most importantly creates a request to download an image. When the connection has finished loading I set the image attribute of the Article object.
The goal is to initialize the cell.imageView.image property with the downloaded image. At that point, you may have guessed what the problem is about.
Images are downloaded after the cell image is rendered so it stays empty until the cell is reloaded (if it gets out of the screen and then back in for example).
I guess I should call reloadData at a certain time but I don't know when. Ideally I would call it when all the cells are loaded but it doesn't seem possible.
I've tried a bunch of crazy things like waiting for a cell to load and the try to reload the previous one but it didn't work.
By reading Q&A out here I learned about Apple's LazyTableImage sample code but I don't understand all of it so I'm not sure I should/could use it.
Please ask me if you need more details.
Thanks in advance for any help.
You have to load the images Asynchronously. Keep in mind that your table data should contain a field per row specifying if the image was loaded so that (if true) you can skip loading and just display cached images.
Look at this question first: Load images async
There are about 10 good Lazy Loading Image UITableView tutorials. Pick one.
You'll get the hang of it by reviewing the tutorials after a while. Stick with it, it's an important concept, not only for this project, but for the rest of your programming career.
Lazy Loading Image UITableView Tutorials that'll make you smarter!

deleting image heavy custom uitableviewcells

I have a table which is made from custom UITableViewCells which contains downloaded images.
Ive found the app crashes after too many images are displayed. How can best stop this? I don't mind deleting the first images, but don't know of the best way to do it.
EDIT
How do I write the images to the device for caching?
Post your cellForRowAtIndexPath code. Sounds like you have a problem there. If you're certain you do not, then when you receive the memory warning, release any objects you do not need, and can easily be loaded again if required. These objects may be in other ViewControllers not on screen or ImageView objects already displayed.
Best we can do unless you post code.
Or read Apple's Memory Management Guide.

Adding a lot of data to core data while keeping the UI responsive

I'm looking for a way to add a whole load of data to core data while keeping the little activity indicator spinning on the UI. I tried adding the data on another thread but since learned that core data is not thread safe, and I get all kinds of errors. Can anyone suggest another approach to this?
Thanks in advance.
Core Data is not thread-safe, but that just means you have to code appropriately, rather avoiding them entirely. To keep your UI responsive/up-to-date, you'll need to use threads.
Apple's documentation on the subject is here, and this blog post is an excellent walk-through of using multiple threads with Core Data, and some of the pitfalls involved.
You need to use a separate managed object context for each thread. There's some additional work you need to do to make changes from other contexts available to your main thread's context: see Concurrency with Core Data for a full discussion.
Most useful example is a core data XML downloading from iOS sample code. If u don't have access, please let me know and I will write here is a main structure of it.
There is a complex custom delegate techniques from u UIviewController to nsoperation delegate, this is important for u to start showing content immediately to user and show a progress of sync.
In AppStore u can see my "snow IXC" app, where u can see this techniques implemented for indicate user in their UIviewController about progress. It's free for downloading.
This is how I avoid background threads with a loading indicator (I use DSActivityView but this should work with other implementations):
In your code when you are going to be displaying the indicator run all the code to show the indicator first. After that code have a separate method call to do all the loading work. Call it by using the method:
[self performSelector:#selector(loadMethodName) withObject:nil afterDelay:0.0];
Normally the app will go straight into the loading code without waiting to show the indicator view. By calling it this way it will first finish displaying the indicator before it moves into the loading code.

How to load images from server to tableView efficiently?

I have a table view with all cells having the UITableViewCellStyleSubtitle,
the images of all the cells are got from the server.
However, those images are not changed frequently.
Someone can show me how to improve the user experience? Each time, user scroll down the table, it seems that it goes online to check and download images again.
Or at least, show me some options that are available to achieve the goal.
Thanks,
The Three20 library has an ImageView subclass that accepts a URL to your remote image and uses the excellent TTURLRequest/Caching mechanism to fetch images. It maintains an in-memory and on-disk cache and will only download images if they are not cached or have expired. You can configure the default cache-expiration time or use a value from your HTTP response. If you use the TTTableViewController subclass and the appropriate TTTableItem subclass, you will get the appropriate image downloading behavior for free. However, it is not necessary to use every three component to do what you need. If you're integrating into existing code, you could create your own UITableViewCell subclass that uses a TTImageView instead of the standard UIImageView. Then, in your cell configuration methods, you can set a default placeholder image and a URL to load and it will pretty much take care of the rest. As a performance optimization, you should also implement the UIScrollView delegate methods in your tableview controller to suspend the TTURLRequestQueue during scrolling (take a look at the TTTableViewController to see how this is done).
You could try one of the following
Create an dictionary and cache all the fetched images in it using the image name as the key
Cache and reuse the entire UITableView cells in tableView:cellForRowAtIndexPath:
If it is just one image repeating load it one outside tableView:cellForRowAtIndexPath:
You write that the images change on a monthly basis - you could save the images to disk as they are used and just either check if the images have changed on the back burner or at a given daily interval redownload the images. Brian Chapados reply seems interesting .
Depending on your code there is probably a ton of other ways to improve image loading. Hope that helps...