I have a iPhone program that has a UIImage. This UIImage needs to be transferred to a javascript Image object in a UIWebView. I was thinking this could be done by using a data url I send to the UIWebView like this:
[wview stringByEvaluatingJavaScriptFromString:@"loadimage('%d')",dataurlfromuiimage];
So, I need to transfer my UIImage into a Data: URL. I could do this myself if I can just get the PNG data, but I cannot find how do do that either. If there is a better way to send this to the WebView, that would be good also.
To get an NSData representation of your image in the PNG format, use
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);
Likewise, a JPEG representation can be obtained using
NSData *dataForJPEGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
Once you have the NSData, you could write it to your Documents directory using writeToFile:atomically:, and then I believe you can pass it in as a local URL (although I've not tried this). An alternative is to use the Base64 NSData category that François P. references and somehow send it to JavaScript as Base64.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With