I've got a db on a server and want to get some data fro my iphone. I use TouchJson and everything works fine but I've got a little problem. I don't know how to download images. When I try to build and run the app the emulator just crashes. Got any ideas what to do?
Asked
Active
Viewed 1,989 times
3 Answers
4
If your JSON request provides a URL to the image then try:
NSString *path = @"http://sstatic.net/so/img/logo.png";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];

Kevin Sylvestre
- 37,288
- 33
- 152
- 232
-
I never thought of using json to simply point to a url of an image. Thats clever insight – Gevious Mar 06 '12 at 19:31
2
Kevin already provided a solution for static images, but if you want to transmit dynamically generated images, you can use the same method with a data: URL.
You'd have to encode your image in Base64 (on the server), then on the client you just have to create a
<img src="data:image/png;base64,the_base64_string_here">
on the client (if using HTML5) or
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:@"data:image/png;base64,the_base64_string_here"]]
(if writing native code).

jcayzac
- 1,441
- 1
- 13
- 26
-1
When you get the path to the image from the db you can simply use UIImage
s -imageWithContentsOfFile:
.

flohei
- 5,248
- 10
- 36
- 61