在我的iOS应用程序中集成jquery javascript项目

在我的iOS应用程序中集成jquery javascript项目,javascript,ios,uiwebview,uicolor,Javascript,Ios,Uiwebview,Uicolor,我试图在图像中获得突出的颜色。起初,我试图将颜色设置为中的颜色。但我的客户并不满意,因为它只返回平均颜色集,而不是确切的颜色。因此,现在我试图通过在中找到的逻辑获取主色集,但问题是代码是jQuery和JavaScript。 似乎它很容易在web上实现,因为代码非常简单 myImage = $('#myImage'); dominantColor = getDominantColor(myImage); paletteArray = createPalette(myImage, 10); // 2

我试图在图像中获得突出的颜色。起初,我试图将颜色设置为中的颜色。但我的客户并不满意,因为它只返回平均颜色集,而不是确切的颜色。因此,现在我试图通过在中找到的逻辑获取主色集,但问题是代码是jQuery和JavaScript。 似乎它很容易在web上实现,因为代码非常简单

myImage = $('#myImage');
dominantColor = getDominantColor(myImage);
paletteArray = createPalette(myImage, 10); // 2nd argument sets # of colors in palette
我浏览了如何在iOS中执行JS,并参考和做了如下操作

[_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]);
[\u webView loadHTMLString:@”“baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]resourcePath]];
NSString*imgName=[[NSBundle mainBundle]pathForResource:@“4”类型:@“jpg”];
//UIImage*img=[UIImage ImageName:imgName];
int colorCount=9;
NSString*function1=[[NSString alloc]initWithFormat:@“createPalette(%@,%d)”,imgName,colorCount];
//NSString*function2=[[NSString alloc]initWithFormat:@“getAverageRGB(%@)”,imgName];
NSString*result=[\u webView stringByEvaluatingJavaScriptFromString:function1];
NSLog(@“%@result%@”,result,[\u webView stringByEvaluatingJavaScriptFromString:function1]);

但是我没有得到任何结果。它是空的。我错过了什么?有人能告诉我哪里出了问题吗?任何及时的帮助都将不胜感激。提前感谢。

您确定您的网页确实加载了jQuery库(从磁盘或在线)


您可以使用safari调试webview,以检查在模拟器或设备上打开webview时出现的问题。让你开始。

我也有同样的问题。您需要一些不同的函数,在getPalette的末尾显式地生成
HTMLImageElement
.toString()
。还有一些奇怪的狩猎行为,它只适用于第二次射击:

let function = "var colorThief = new ColorThief(); var image = new Image(); image.src='\(imgName)'; colorThief.getPalette(image, 5).toString();"

let result1 = self.webView.stringByEvaluatingJavaScriptFromString(function)!
// for some reason result1 is always empty, but if we evaluate the same function second time after some time interval we will get the results.

sleep(1)

let result2 = self.webView.stringByEvaluatingJavaScriptFromString(function)!
// result2 is string of rgb components separated by commas