【发布时间】:2012-07-31 11:27:27
【问题描述】:
我正在尝试获取图像中的突出颜色。最初我试图在this post 中设置颜色。但我的客户对此并不满意,因为它只返回平均颜色集,而不是确切的颜色。因此,现在我试图通过this link. 中的逻辑设置主要颜色,但问题是代码在 jQuery 和 JavaScript 中。 看起来很容易在网络中实现,因为代码非常简单
myImage = $('#myImage');
dominantColor = getDominantColor(myImage);
paletteArray = createPalette(myImage, 10); // 2nd argument sets # of colors in palette
我浏览了如何在iOS中执行JS并参考this link和this link做了如下
[_webView loadHTMLString:@"<script src=\"color-thief.js\"></script>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
NSString * imgName = [[NSBundle mainBundle] pathForResource:@"4" ofType:@"jpg"];
// UIImage * img = [UIImage imageNamed:imgName];
int colorCount = 9;
NSString *function1 =[[NSString alloc] initWithFormat: @"createPalette(%@, %d)", imgName, colorCount];
// NSString *function2 = [[NSString alloc] initWithFormat: @"getAverageRGB(%@)", imgName];
NSString *result = [_webView stringByEvaluatingJavaScriptFromString:function1];
NSLog(@"%@ result %@", result, [_webView stringByEvaluatingJavaScriptFromString:funcion1]);
但我无法得到任何结果。其为空。我错过了什么?有人可以指出我出了什么问题吗?任何及时的帮助表示赞赏。提前致谢。
【问题讨论】:
标签: javascript ios uiwebview uicolor