来自UIImage的数据URL / PNG [英] Data URL / PNG from UIImage
问题描述
我有一个iPhone程序,有一个UIImage。这个UIImage需要传递到UIWebView中的一个javascript Image对象。我认为这可以通过使用数据url我发送到UIWebView这样做:
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];
因此,我需要将UIImage转换为数据:URL。我可以自己做,如果我只能得到PNG数据,但我不能找到如何做到这一点。如果有更好的方式发送到WebView,这也是很好的。
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.
推荐答案
您的图片格式为PNG,请使用
To get an NSData representation of your image in the PNG format, use
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);
同样,使用
NSData *dataForJPEGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
一旦你有了NSData,你可以使用writeToFile:atomically:然后我相信你可以传递它作为本地URL(虽然我没有尝试过)。一个替代方法是使用Base64 NSData类别,FrançoisP.引用并以某种方式将其作为Base64发送到JavaScript。
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.
这篇关于来自UIImage的数据URL / PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!