how to download photos from remote server to iphone app? - iphone

My situation is that I am making a uitableviewcontroller with uitableviewcells having a thumb nail or picture on the left, text on the right. All data is stored in a remote server. I have made a php to communicate with backend batabase and generate JSON to pass the data to iphone through http. However, I don't know how to do the same thing to photos. Please suggest me someway to do it. Thanks!

As Mundi said it already.
And make sure that you load them asynchronously.
There is a nice tutorial on this page:
http://www.markj.net/iphone-asynchronous-table-image/

I assume you are using NSURLConnection to fetch your data from the server. Do this:
Collect the photo data into an NSData object.
In the didFinishLoading method use this to create the images:
UIImage *thumbnailImage = [[UIImage alloc] initWithData:downloadedData];
// don't forget to discard the downloaded Data and release the image
The actual setup of your cells might be more complicated. The way I do it is to check in each call to cellForRowAtIndexPath: if I have cached the image, if not I pass to URL of the image to a NSURLConnection. I keep the open connections in an array and eliminated them when each download finishes or the view disappears. Let me know if you need more hints...

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.

How to download and load downloads image in Custom UITableViewCell?

I get the text form the sever like this:
test_abc
http://www.xxx.a.jpg
test_aaa
http://www.xxx.b.jpg
test_ccc
I want to split the string to ["test_abc", "http://www.xxx.a.jpg", "test_aaa", "http://www.xxx.b.jpg", "test_ccc"] and then display them in a UITableViewCell,before the images will be downloaded, I used a LoadingIndicator as placeholder, when the images will be downloaded, display the images and modify the height of the cell to display the whole images.
Can someone help me do this?
Try looking into Three20
An open source library written by the guy who wrote the official "Facebook" app for iOS. Moreover, the code of the facebook app is based on that library.
You may use SDWebImage, and you don't have to worry about cache, etc..
Web Image
This library provides a category for UIImageVIew with support for remote images coming from the web.
It provides:
An UIImageView category adding web image and cache management to the Cocoa Touch framework
An asynchronous image downloader
An asynchronous memory + disk image caching with automatic cache expiration handling
A guarantee that the same URL won't be downloaded several times
A guarantee that bogus URLs won't be retried again and again
Performances!

Show images asynchronouly in UITableView from server in iPhone

When I try to fetch the images in UITableView using NSURLUIImage image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://mysite.com/mobile/abc.jpg]]];
my application get freezes until the images loaded. Also when I scroll the table it get images from the server again and scroll get stuck for some time.
I know this is happening because of downloading images synchronously from server.
Is there any solution so that I can download the image asynchronously from server and store it locally or in cache so that when table scrolls it did not get images again from server.
Thanks
Check out EGOImageLoading by enormego. Works just like UIImage (and caches images) but lets you load from HTTP asynchronously.
You can create your own class which leverages NSURLConnection and it's various Asynchronous methods to load your images in the background and then have a callback to display it in the cell.
However, there are some excellent libraries which do exactly what you've described. HJCache is one i've used and recommend for this sole purpose

UIImageView Image from NSURLConnection

I have a function that retrieves some images from a JSON feed using NSURLConnection. My connectionDidFinishLoading retrieves an array with all the data for that image (including the URL of the image).
What's the best way to display that image on a UIImageView? Even though the URL is being retrieved, I am pretty sure I would still have to display the image in a connection-friendly manner?
Thank you,
As per your requirement Images must be display in connection friendly manner. Then you can go for Image cacheing which requires image URLs. For your best understanding see this link and this link.
for asynchronous image downloading try with this link.

Saving images in Document Directory

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.