• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Swift版本UIWebView长按保存图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

起因

最近需要做个IOS的壳子,用到长按保存图片的功能,发现百度出来的全是OC语法的例子,很多都不是全面,只能自己写一份Swift版本的,图片下面附上Github地址


效果图

 

Github地址:https://github.com/goyuanfang/SwifLongTouchSavePic


原理

UIWebView中 

func webViewDidFinishLoad(webView: UIWebView) 

加载完URL后注入监听手机触摸屏幕的js脚本:

 

document.ontouchstart=function(event){
x=event.targetTouches[0].clientX;
y=event.targetTouches[0].clientY;
document.location="myweb:touch:start:"+x+":"+y;
};
document.ontouchmove=function(event){
x=event.targetTouches[0].clientX;
y=event.targetTouches[0].clientY;
document.location="myweb:touch:move:"+x+":"+y;};
document.ontouchcancel=function(event){
document.location="myweb:touch:cancel";
};
document.ontouchend=function(event){
document.location="myweb:touch:end";
};

 每次监听到手指移动都会将修改document的页面定向,将参数传递给Swift的

webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType)

捕获到页面定向后分析参数,读取到img的src,弹出保存对话框


代码

enum TouchState {
    case None
    case Start
    case Move
    case End
    case Cancel
}
//脚本触摸事件
static var touchJSStr:String = "document.ontouchstart=function(event){x=event.targetTouches[0].clientX;y=event.targetTouches[0].clientY;document.location=\"myweb:touch:start:\"+x+\":\"+y;};document.ontouchmove=function(event){x=event.targetTouches[0].clientX;y=event.targetTouches[0].clientY;document.location=\"myweb:touch:move:\"+x+\":\"+y;};document.ontouchcancel=function(event){document.location=\"myweb:touch:cancel\";};document.ontouchend=function(event){document.location=\"myweb:touch:end\";};"
static var imgUrl:String = ""//存储当前点击的图片路径
var touchState:TouchState = TouchState.None//设置默认的点击状态为NONE
var timer:NSTimer? = nil//定时器 长按时 定时器启动 执行一次 弹出保存确认

ViewController要继承UIWebViewDelegate,UIActionSheetDelegate

 @IBOutlet weak var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.delegate = self
        webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://image.baidu.com")!))
 }
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool{
        let requestStr:String = request.URL!.absoluteString
        let components = requestStr.componentsSeparatedByString(":")
        if(components.count>1 && components[0] == "myweb"){
            if(components[1] == "touch"){
                if(components[2] == "start"){
                    touchState = TouchState.Start
                    let ptX:Float32 = (components[3] as NSString).floatValue
                    let ptY:Float32 = (components[4] as NSString).floatValue
                    let js:String = "document.elementFromPoint(\(ptX), \(ptY)).tagName"
                    let tagName:String? = webView.stringByEvaluatingJavaScriptFromString(js)
                    if(tagName!.uppercaseString == "IMG")
                    {
                        let srcJS:String = "document.elementFromPoint(\(ptX), \(ptY)).src"
                        ViewController.imgUrl = srcJS
                        if(ViewController.imgUrl != ""){
                            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "handleLongTouch", userInfo: nil, repeats: false)
                        }
                    }
                }else if(components[2] == "move"){
                    touchState = TouchState.Move
                    if(timer != nil)
                    {
                        timer!.fire()
                    }
                }
                else if(components[2] == "cancel"){
                    touchState = TouchState.Cancel
                    if(timer != nil)
                    {
                        timer!.fire()
                    }
                }
                else if(components[2] == "end"){
                    touchState = TouchState.End
                    if(timer != nil)
                    {
                        timer!.fire()
                    }
                }
            }
        }
        return true
    }
    
    func webViewDidFinishLoad(webView: UIWebView){
        webView.stringByEvaluatingJavaScriptFromString(ViewController.touchJSStr)//触摸js注册
    }
    //弹出保存对话框
    func handleLongTouch(){
        if(ViewController.imgUrl != "" && touchState == TouchState.Start){
            var sheet:UIActionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "保存图片")
            sheet.cancelButtonIndex = sheet.numberOfButtons - 1
            sheet.showInView(UIApplication.sharedApplication().keyWindow!)
        }
    }
    //按钮点击保存 保存图片 需要实现 UIActionSheetDelegate
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int){
        if(buttonIndex == 1){
            let urlToSave:String? = self.webView.stringByEvaluatingJavaScriptFromString(ViewController.imgUrl)
            let data:NSData? = NSData(contentsOfURL: NSURL(string: urlToSave!)!)
            var image:UIImage? = UIImage(data: data!)
            UIImageWriteToSavedPhotosAlbum(image!, self, "image:didFinishSavingWithError:contextInfo:", nil)
        }
    }
    
    func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject) {
        if didFinishSavingWithError != nil {
            return
        }
    }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用Pods中使用Swift和Objective-C混编-编译不通过的原因发布时间:2022-07-13
下一篇:
Swift中出现“nosuchmodulecocoa”的错误发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap