//添加长按手势
UILongPressGestureRecognizer *lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
//长按时长 1秒
lp.minimumPressDuration = 1;
lp.delegate = self;
[self.WKView addGestureRecognizer:lp];
#pragma mark - 保存图片
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender{
if (sender.state != UIGestureRecognizerStateBegan) {
return;
}
CGPoint touchPoint = [sender locationInView:self.WKView];
CGFloat ptX, ptY;
ptX = touchPoint.x;
ptY = touchPoint.y;
[self.WKView evaluateJavaScript:[NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", ptX, ptY] completionHandler:^(id _Nullable response, NSError * _Nullable error) {
NSString * tagName = response;
if ([tagName isEqualToString:@"IMG"]) {
[self->_WKView evaluateJavaScript:[NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", ptX, ptY] completionHandler:^(id _Nullable response, NSError * _Nullable error) {
NSString * imgUrl = response;
if (imgUrl) {
UIImage *img = [self downloadImage:imgUrl];
if (!img) {
return;
}
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
}];
UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
UIImageWriteToSavedPhotosAlbum(img,self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
}];
[alert addAction:cancelAction];
[alert addAction:saveAction];
[self presentViewController:alert animated:YES completion:nil];
}
}];
}
}];
}
- (UIImage *)downloadImage:(NSString *)urlString {
NSURL *url = [NSURL URLWithString: urlString];
UIImage *img;
NSData *data = [NSData dataWithContentsOfURL:url];
img = [UIImage imageWithData:data];
if (!img) {
//MBPSHOW(@"读取图片失败");
NSLog(@"读取图片失败");
return nil;
}
return img;
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error != NULL) {
//MBPSHOW(@"图片保存失败");
NSLog(@"图片保存失败");
}else
{
// MBPSHOW(@"图片保存成功");
NSLog(@"图片保存成功");
}
}
iOS WKWebView添加长按手势保存H5图片
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- WKWebView 加载网页点击保存图片 我这里只提一下我发小的一个小技巧,之前使用UIWbeview加载HTML...
- 1、截屏 给网页加个长按手势,记得添加代理否则手势无法识别。 手势事件 保存图片 识别二维码 识别器的几种类型 2...
- 话不多说,直接上代码 WKWebViewConfiguration *configuration = [[WKW...