Display image on UIImageView using Web server image

63 views Asked by At

I want to display an image on UIImageView calling the Web server image. Because I want to replace and show it right away.

here's the code

NSURL *imageURL = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
webImage.image = [UIImage imageWithData:imageData];

The problem is... The image is displayed well at first, but if I replaced the image on the web server with another image.(The file name is the same, only the file is replaced) It didn't change and still displayed the first image.

Please help me...

1

There are 1 answers

1
The Dreams Wind On

The code in the question seems flawed, i.e. you apparently do a network request in the UI thread. Such approach tends to make your application UI unresponsive. However, if we boil down the question to "Why NSData returns outdated data for the same URL?" then the answer is because it has internal caching. The caching logic could controlled either by the the response headers from the server (e.g. Expires) or with NSDataReadingUncached option (to suppress any caching logic):

NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];