• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

javascript - 在 Obj C 中,如何添加 SVG 蓝图图像并从 UIWebView 渲染数据?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:25:51 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我已将 SVG 图像加载到 UIWebView 中,但没有发生从 SVG 图像的选择位置渲染数据的工作魅力。即使我被搜索了更多关于我没有得到任何关于这个功能的信息。同样,我尝试过 SVGKit,但我不了解 SVGKit 的完整场景,所以我选择 UIWebView 的原因。如果有人知道这个功能,请在此处发表评论。

the SVG map seems like below one.i found few link about SVG image but those are not loading by UIWebView. SVGKIT & chensheng12330/SVGKitDemo

enter image description here



Best Answer-推荐答案


//Create a empty test.html and override below html code and reload the Webview.   
   -(UIWebView *)setup{
        self.svgName=@"third";
        //create a webview
        UIWebView *webview = [[UIWebView alloc] init];
        //create a empty html file and convert the svg into html and save dynamically
        NSString *testPath = [[NSBundle mainBundle]pathForResource"test"ofType"html"inDirectory:nil ];
        NSString *filePath = [self writeAndAppendString:[self getHTMLStringForLoadSVG:[self.svgName stringByAppendingString".svg"]] toFile:testPath];
        //load svg file into the webview
        //NSString *path = [[NSBundle mainBundle] pathForResource"loadSVG" ofType"html"];
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
        NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
        [ webview loadRequest:req];
        [ webview setScalesPageToFit:YES];
        [webview setUserInteractionEnabled:NO];
       // webview.contentMode = UIViewContentModeScaleAspectFit;
        [webview setBackgroundColor:[UIColor cyanColor]];
        webview.delegate = self;
        return webview;
    }
    //HTML file
    -(NSString *)getHTMLStringForLoadSVG NSString *)svgFileName{
        NSString *jQueryLib = @"https://code.jquery.com/jquery-3.2.1.min.js";
        NSString *idOfJQuery = @"test";
        NSString *type = @"image/svg+xml";
        NSString *id = @"test";
        return [NSString stringWithFormat"<!DOCTYPE html>\n<html>\n<head>\n<script src='%@'></script>\n<script>\n  window.clickedId;\n$(document).ready(function() {\nvar embed = document.getElementById('%@');\nembed.addEventListener('load', function(){\nvar svg = embed.getSVGDocument();\nvar pathElements = svg.getElementsByTagName('path');\nfor(var i=0;i<pathElements.length;i++){\n$(pathElements[i]).bind('click', function() {\nwindow.clickedId = $(this).attr('id');\n});\n}});\n});\nfunction clicked(){\n return window.clickedId;\n}</script>\n</head>\n<body>\n<embed src='%@' type='%@' id='%@'/>\n</body>\n</html>", jQueryLib,idOfJQuery,svgFileName,type,id];
    }
    - (void)tapActionUITapGestureRecognizer *)sender{
        [self performSelectorselector(getJSObj) withObject:nil afterDelay:1.0];
    }
    -(void)getJSObj{
        NSLog(@"return value :%@",[self.self.mapWebview stringByEvaluatingJavaScriptFromString"clicked()"]);
    }


    //write the code of converting svg to html code dynamically
    - (NSString *)writeAndAppendStringNSString *)str toFileNSString *)fileName {
        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:fileName]) {
            NSString *testP = [[paths firstObject] stringByAppendingPathComponent"testHtml.html"];
            NSString *testPath = [[NSBundle mainBundle]pathForResource:svgName ofType"svg"inDirectory:nil ];
            NSString *testSvgP = [[paths firstObject] stringByAppendingPathComponent:[svgName stringByAppendingString:@".svg"]];
            [[NSFileManager defaultManager] copyItemAtPath:testPath toPath:testSvgP error:nil];
            [[NSFileManager defaultManager] copyItemAtPath:fileName toPath:testP error:nil];
            // Add the text at the end of the file.
            NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:testP];
            //  [fileHandler seekToEndOfFile];
            [fileHandler writeData:data];
            [fileHandler closeFile];
            return testP;
        }
        return nil;
    }

关于javascript - 在 Obj C 中,如何添加 SVG 蓝图图像并从 UIWebView 渲染数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43830427/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap