views:

144

answers:

3
+2  Q: 

Json and images

hi, 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?

A: 

When you get the path to the image from the db you can simply use UIImages -imageWithContentsOfFile:.

flohei
+2  A: 

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
+2  A: 

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