Json and images

I have a db on the server and want to get some data for my iphone. I am using TouchJson and everything works fine, but I have a small problem. I don't know how to upload images. When I try to build and run my application, the emulator just crashes. Any idea what to do?

+2


a source to share


3 answers


If your JSON request contains an image url 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];

      

+4


a source


Kevin already provided a solution for static images, but if you want to pass dynamically generated images you can use the same method with the data url.

You will need to encode your image in Base64 (on the server) and then on the client you just need to create

<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"]]

(when writing your own code).

+2


a source


When you get the path to the image from db you can just use UIImage

-imageWithContentsOfFile:

.

-1


a source







All Articles