【问题标题】:Integrating jquery-javascript project in my iOS app在我的 iOS 应用程序中集成 jquery-javascript 项目
【发布时间】: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 linkthis 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


    【解决方案1】:

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

    您可以使用 safari 调试 webview,以检查在模拟器或您的设备上打开 webview 时出现的问题。 Here's a guide from apple 让您开始。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。您需要一些不同的功能,在 getPalette 的末尾显式创建 HTMLImageElement.toString()。还有一些奇怪的 Safari 行为,它只适用于第二次拍摄:

      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
      

      【讨论】:

        猜你喜欢
        • 2014-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多