美文网首页
获取webview的图片下载并保存

获取webview的图片下载并保存

作者: 念念不忘的 | 来源:发表于2016-12-15 16:03 被阅读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];
    }
}

相关文章

网友评论

      本文标题:获取webview的图片下载并保存

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