gpt4 book ai didi

ios - swift/iOS |如何启用 'press and hold' 以在 UIWebView 中保存图像?

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:00 25 4
gpt4 key购买 nike

我现在正在编写一个应用程序,它可以从我的服务器加载文章列表和特定文章。我正在使用 UIWebView 加载文章内容(女巫是一个 HTML 字符串)。现在的问题是,当用户按住 UIWebView 中的图像时,会跳出一个操作表并为用户提供保存图像 选项。

这些我都试过了,但都不适合我。

stackoverflow.com/questions/31522037/tap-and-hold-to-save-image-in-uiwebview

stackoverflow.com/questions/31673916/tap-and-hold-save-image-from-uiwebview

buzztouch.com/forum/thread.php?tid=7EF2CABD19B91FB8D9C20E6&currentPage=1

最佳答案

是的!!!!!!!我终于做到了!!!!通过使用 JavaScript 和 stringByEvaluatingJavaScriptFromString

我在 github 上发布了我的答案:https://github.com/theniceboy/HoldToSaveImage

这是我的代码:

//
// FrmArticle.swift
// CenterBrain
//
// Created by David Chen on 9/7/15.
// Copyright © 2015 David Chen. All rights reserved.
//

import UIKit

class FrmArticle: UIViewController, UIWebViewDelegate {

// MARK: - Outlets
@IBOutlet weak var wvContent: UIWebView!

// MARK: - Vars & Lets
var article_id: Int = 1
var article: Article = Article()

let kTouchJavaScriptString: 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\";};"
var _gesState: Int = 0
/*
_gesState:
GESTURE_STATE_NONE = 0,
GESTURE_STATE_START = 1,
GESTURE_STATE_MOVE = 2,
GESTURE_STATE_END = 4
*/

var _imgURL: String = "", _timer: NSTimer = NSTimer()

func webView(webView: UIWebView, shouldStartLoadWithRequest _request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if (_request.URL! == "about:blank") {
return false
}
let requestString: String = (_request.URL?.absoluteString)!
var components: [String] = requestString.componentsSeparatedByString(":")
if (components.count > 1 && components[0] == "myweb") {
if (components[1] == "touch") {
if (components[2] == "start") {
_gesState = 1
print("touch start!")
let ptX: Float = Float(components[3])!
let ptY: Float = Float(components[4])!
print("touch point \(ptX), \(ptY)")
let js: String = "document.elementFromPoint(\(ptX), \(ptY)).tagName"
let tagName: String = wvContent.stringByEvaluatingJavaScriptFromString(js)!
_imgURL = ""
print(tagName)
if (tagName == "IMG") {
_imgURL = wvContent.stringByEvaluatingJavaScriptFromString("document.elementFromPoint(\(ptX), \(ptY)).src")!
_timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "handleLongTouch", userInfo: nil, repeats: false)
}
} else {
if (components[2] == "move") {
self._gesState = 2
print("you are move")
} else {
if (components[2] == "end") {
_timer.invalidate()
self._timer = NSTimer()
self._gesState = 4
print("touch end")
}
}
}
}
return false
}
return true
}

func handleLongTouch() {
print(_imgURL)
if (_gesState == 1) {
print("YES!!! I DID IT!!!")
}
}

// MARK: - Override functions
override func viewDidLoad() {
wvContent.delegate = self
super.viewDidLoad()
wvContent.loadHTMLString("<h2 align=\"center\">Title</h2><img src=\"http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=ea71a5211a91&a\">", baseURL: nil)
}

// MARK: - UIWebView delegate
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
activityIndicator.hidden = true
wvContent.stringByEvaluatingJavaScriptFromString(kTouchJavaScriptString)
}
}

关于ios - swift/iOS |如何启用 'press and hold' 以在 UIWebView 中保存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32435194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com