我现在正在编写一个应用程序,可以加载文章列表以及我服务器上的特定文章。我正在使用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¤tPage=1
答案 0 :(得分:3)
YES !!!!!!!我终于完成了!!!!!使用JavaScript和//
// 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)
}
}
。
我在github上发布了我的答案:https://github.com/theniceboy/HoldToSaveImage
这是我的代码:
<context:property-placeholder location="classpath:file.properties"/>