美文网首页
UIWebView保存图片

UIWebView保存图片

作者: Helen_kay | 来源:发表于2018-03-13 11:13 被阅读0次

    要保存网页中的图片,关键是要获取手指点击位置的图片的url地址,这就需要从js调用oc的方法。下面介绍两种方法来实现图片保存功能,但是这两种方法都只适用于图片地址用如下形式表示才可以获取:
    [图片上传失败...(image-71c880-1520910783696)]

    如果图片是通过js动态生成的,就无法使用下面的方法获取

    方法1、获取点击位置的图片的src属性

    实现原理:
    该方法主要是获取手指点击的位置,然后获取该位置的标签的src属性,如果是img标签,那么就可以获取到url。如果是其他标签,就无法获取到url属性。

            //在分线程中下载、缓存、读取  在主线程中显示图片
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSData* data = [self saveServiceImage:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kImageServerRootURL, [dic valueForKey:@"serviceIconUrl2"]]]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (data) {
                        cell.titleImage.image = [UIImage imageWithData:data];
                    }
                    else {
                        cell.titleImage.image = [UIImage imageNamed:@"erweima"];
                    }
    
    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
    {
    
        NSString* requestString = [[request URL] absoluteString];
    
        NSArray* components = [requestString componentsSeparatedByString:@":"];
    
        if ([components count] > 1 && [(NSString*)[components objectAtIndex:0] isEqualToString:@"lfyprotocol"]) {
            if ([(NSString*)[components objectAtIndex:1] isEqualToString:@"http"] || [(NSString*)[components objectAtIndex:1] isEqualToString:@"https"]) {
    
                //            NSString *path = [NSString stringWithFormat:@"%@:%@:%@",[components objectAtIndex:1],[components objectAtIndex:2],[components objectAtIndex:3]];
                NSString* path = [requestString substringWithRange:NSMakeRange(12, requestString.length - 12)];
                NSMutableDictionary* dic = [[[NSMutableDictionary alloc] init] autorelease];
                [dic setObject:path forKey:@"imageURL"];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"browsePhoto" object:nil userInfo:dic];
            }
            return NO;
        }
        if ([self.titleName isEqualToString:NoPaperTitle]) {
    
            if ([[requestString componentsSeparatedByString:@"userInfo"] count] >= 2) {
                NSArray* array = [requestString componentsSeparatedByString:@"="];
                //获取并转换所需参数
                NSString* str = [array[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                NSDictionary* jsonDict = [SystemUtils jumpTowebView:str];
                // 获取fn 字符串转字典
                NSDictionary* fnDic = [imageEnarge JsonStringToDict:array[1]];
                //字典转json
                NSString* jsonStr = [imageEnarge dictionaryToJson:jsonDict];
                //拼接注册函数
                NSString* stri2 = [NSString stringWithFormat:@"%@(%@);", [fnDic objectForKey:@"fn"], jsonStr];
                [webView stringByEvaluatingJavaScriptFromString:stri2];
                return NO;
            }
        }
    
        return YES;
    }
    

    相关文章

      网友评论

          本文标题:UIWebView保存图片

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