2012-07-31 3 views
1

이미지에서 눈에 띄는 색상을 얻으려고합니다. 처음에는 this post처럼 색상을 설정하려고했습니다. 하지만 클라이언트는 정확한 색상이 아닌 평균 색상 세트 만 반환하기 때문에 만족하지 않습니다. 그러므로 이제는 this link.에있는 논리에 따라 지배적 인 색을 설정하려고합니다.하지만 문제는 jQuery와 JavaScript에 문제가 있다는 것입니다. 코드가 나는 아이폰 OS에 JS를 실행하는 방법 서핑과 this linkthis link를 참조했고, 다음과 같이iOS 앱에 jquery-javascript 프로젝트 통합

[_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]); 

했다 매우 간단

myImage = $('#myImage'); 
dominantColor = getDominantColor(myImage); 
paletteArray = createPalette(myImage, 10); // 2nd argument sets # of colors in palette 

로 그대로 웹에서 구현하기 쉬운 것 같습니다 그러나 나는 어떤 결과도 얻을 수 없었다. 그것의 null. 내가 뭘 놓치고 있니? 어떤 점이 잘못되었다고 지적 할 수 있습니까? 시의 적절한 도움을 주시면 감사하겠습니다. 미리 감사드립니다.

답변

0

웹 페이지가 디스크 또는 온라인에서 jQuery 라이브러리를로드 했습니까?

사파리를 사용하여 웹 뷰를 디버깅하여 시뮬레이터 나 장치에서 웹 뷰가 열렸을 때 어떤 문제가 있는지 확인할 수 있습니다. Here's a guide from apple을 시작하십시오.

0

동일한 문제가있었습니다. 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