美文网首页iOS机智小工具
UIWebView---------获取保存webview里面的

UIWebView---------获取保存webview里面的

作者: 乡水情缘 | 来源:发表于2016-10-14 14:25 被阅读567次

1.第一步在 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 获取到图片的URL

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

NSString *url = request.URL.absoluteString;

NSRange range = [url rangeOfString:@"sx:src="];

if (range.location != NSNotFound) {

NSInteger begin = range.location + range.length;

NSString *src = [url substringFromIndex:begin];

[self savePictureToAlbum:src];

return NO;

}

return YES;

}

2.第二步:根据URL 保存图片

- (void)savePictureToAlbum:(NSString *)src

{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要保存到相册吗?" preferredStyle:UIAlertControllerStyleActionSheet];

[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

NSURLCache *cache =[NSURLCache sharedURLCache];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:src]];

NSData *imgData = [cache cachedResponseForRequest:request].data;

UIImage *image = [UIImage imageWithData:imgData];

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}]];

[self presentViewController:alert animated:YES completion:nil];

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

if (error != NULL){

[MBProgressHUD showError:@"下载失败"];

}else{

[MBProgressHUD showSuccess:@"保存成功"];

}

}

3.关键之处在于如何获取图片的点击事件

在这有两种方法(1)使用 js添加点击事件 NSString *onload = @"this.onclick = function() {"        "  window.location.href = 'sx:src=' +this.src;"        "};";        [imgHtml appendFormat:@"

",onload,width,height,detailImgModel.src];

第二种方法:采用js与源生交互

相关文章

网友评论

    本文标题:UIWebView---------获取保存webview里面的

    本文链接:https://www.haomeiwen.com/subject/ozggyttx.html