获取webview的图片下载并保存
2016-12-15 本文已影响221人
念念不忘的
// 获取图片
- (void)getWeChatQrcode
{
static NSString * const jsGetImages =
@"function getImages(){\
var objs = document.getElementsByTagName(\"img\");\
var imgScr = '';\
for(var i=0;i<objs.length;i++){\
imgScr = imgScr + objs[i].src + '+';\
};\
return imgScr;\
};";
[_wkWeb evaluateJavaScript:jsGetImages completionHandler:^(id _Nullable result, NSError * _Nullable error) {
}];
[_wkWeb evaluateJavaScript:@"getImages()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSArray *array = [NSArray arrayWithArray:[result componentsSeparatedByString:@"+"]];
qrcodeImgStr = array[0];
}];
}
// 保存图片
- (void)saveQrcodeImage
{
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:qrcodeImgStr]]];
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error == nil) {
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
[SVProgressHUD showSuccessWithStatus:@"已保存到相册"];
[self performSelector:@selector(hudHide) withObject:self afterDelay:1.5f];
} else {
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
[SVProgressHUD showErrorWithStatus:@"保存失败"];
[self performSelector:@selector(hudHide) withObject:self afterDelay:1.5f];
}
}