UIWebView---------獲取保存webview里面的圖片保存到手機(jī)相冊

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.第二步:根據(jù)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.關(guān)鍵之處在于如何獲取圖片的點(diǎn)擊事件

在這有兩種方法(1)使用 js添加點(diǎn)擊事件 NSString *onload = @"this.onclick = function() {"? ? ? ? "? window.location.href = 'sx:src=' +this.src;"? ? ? ? "};";? ? ? ? [imgHtml appendFormat:@"

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

第二種方法:采用js與源生交互

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容