开发者

Json and images

开发者 https://www.devze.com 2022-12-29 13:54 出处:网络
I\'v开发者_JS百科e 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

I'v开发者_JS百科e 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?


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 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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号