Swift4.0版-H5页面实现长按保存图片
- 刚开始拿到需求还是比较尴尬的,不会啊,没做过啊,是不是就尴尬了
- 随即想了一下,好像微信里好多H5页面都有这样的功能
- 然后查阅了一下相关资料,发现两行核心代码
//获取长按所在点
let urlString = "document.elementFromPoint(\(touchPoint.x), \(touchPoint.y)).src"
//根据该点的参数获取对应图片的链接
let saveUrl = webView.stringByEvaluatingJavaScript(from: urlString)
下面来一起啊看一下完整的代码步骤
首先给UiWebView加一个长按手势
//添加长按手势
let longPressGes = UILongPressGestureRecognizer(target: self, action: #selector(longPressedGesture(_:)))
//一定要遵循代理
longPressGes.delegate = self
webView.addGestureRecognizer(longPressGes)
实现代理方法
//不实现该代理方法,长按无效
//MARK: 手势代理方法
extension SIWebViewController : UIGestureRecognizerDelegate{
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
接着在手势响应方法里面实现相应的功能
注意:
- 一定要判断手势的state属性
- 判断saveUrl是否是一个nil值
@objc func longPressedGesture(recognizer: UILongPressGestureRecognizer){
if recognizer.state != .began { return }
let touchPoint = recognizer.location(in: webView)
//核心代码
let urlString = "document.elementFromPoint(\(touchPoint.x), \(touchPoint.y)).src"
if let saveUrl = webView.stringByEvaluatingJavaScript(from: urlString) {
//判断图片的链接是否为空,长度是否为o
if saveUrl.characters.count == 0 { return }
addAlertAction(saveUrl)
}
}
调用保存图片功能按钮
fileprivate func addAlertAction(imageStr: String){
let alertV = UIAlertController()
let saveAction = UIAlertAction(title: "保存图片", style: .default) { (alertV) in
self.saveImageToDisk(imageStr)
}
//取消保存不作处理
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
alertV.addAction(saveAction)
alertV.addAction(cancelAction)
controller.present(alertV, animated: true, completion: nil)
}
使用SDWebImage保存图片
//保存图片
fileprivate func saveImageToDisk(_ imageStr: String){
guard let imageUrl = URL(string: imageStr) else { return }
let sdManager = SDWebImageManager.shared()
var image : UIImage!
if (sdManager?.diskImageExists(for: imageUrl))! {
//先从缓存中查找图片
image = sdManager?.imageCache.imageFromDiskCache(forKey: imageUrl.absoluteString)
}else{
//缓存中没有图片在进行网络下载
let data = try! Data(contentsOf: imageUrl)
image = UIImage(data: data)
}
//保存图片到相册中
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
最后是一个保存成功与否的回调方法
@objc func image(_ image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject){
if didFinishSavingWithError != nil {
MBProgressHUD.show(string: "请开启访问相册权限后使用此功能", inView: self.view)
} else {
MBProgressHUD.show(string: "图片保存成功", inView: self.view)
}
}
以上如有不妥之处还望多多指正