2017年6月4日 星期日

webwebview could show alert windows

Ref :   https://stackoverflow.com/questions/34185339/wkwebview-javascript-confirm-and-alert-not-working


import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

    var wkWebView: WKWebView!

    public override func viewDidLoad() {
        super.viewDidLoad()

        wkWebView = WKWebView(frame: view.bounds, configuration: WKWebViewConfiguration())
        wkWebView.uiDelegate = self
        wkWebView.navigationDelegate = self
        view.addSubview(wkWebView!)
        let url = URL(string: "https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert")!
        wkWebView.load(URLRequest(url: url))
    }

    func webView(_ webView: WKWebView,
                 runJavaScriptAlertPanelWithMessage message: String,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping () -> Void) {

        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        let title = NSLocalizedString("OK", comment: "OK Button")
        let ok = UIAlertAction(title: title, style: .default) { (action: UIAlertAction) -> Void in
            alert.dismiss(animated: true, completion: nil)
        }
        alert.addAction(ok)
        present(alert, animated: true)
        completionHandler()
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        wkWebView.evaluateJavaScript("alert('Hello from evaluateJavascript()')", completionHandler: nil)
    }
}

2017年5月9日 星期二

uiimage to mat and mat to uiimage

OpenCVWrapper.mm


#import <opencv2/imgcodecs/ios.h>




+(UIImage *) makeGrayFromImage:(UIImage *)image{
    cv::Mat imageMat;
    UIImageToMat(image, imageMat);
    
    if (imageMat.channels() ==1)
        return image;
    
    
    cv::Mat grayMat;
    cv::cvtColor(imageMat, grayMat, CV_BGR2GRAY);
    return MatToUIImage(grayMat);

}

2017年5月5日 星期五

install opencv on mac

ref : http://junwayh.logdown.com/posts/607175


brew install cmake


2. 安裝OpenCV

先到官方網站下載 http://opencv.org/
解壓縮後開終端機cd到解壓縮的資料夾中
mkdir release
cd release
cmake -G "Unix Makefiles" ..
make
sudo make install

3. 建立專案

開啟XCode,選擇Command Line Tool

進入Build Settings介面
往下找到 Library Search Paths 並新增 /usr/local/lib
再來對 header search paths 新增 /usr/local/include 及 /usr/local/include/opencv

直接按下 / 會跳出 go to the folder 輸入 /usr/local/lib
然後把所有opencv的lib都選起來再按 Add 就大功告成
隨便找個sample code跑跑看吧!!
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv ;
using namespace std ;

int main() {
    cv::Mat image = cv::imread("your image path") ;
    cv::namedWindow("Image") ;
    cv::imshow("Image", image) ;
    cv::waitKey() ;
    
    return 0;
}


這邊路徑要用絕對路徑


cv::Mat image = cv::imread("/Users/abc/Desktop/opencv_test/test.jpg",CV_LOAD_IMAGE_GRAYSCALE) ;