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

Javascript 在Obj C中,如何添加SVG蓝图图像并从UIWebView渲染数据?,javascript,ios,objective-c,svg,uiwebview,Javascript,Ios,Objective C,Svg,Uiwebview,我已经将SVG图像加载到UIWebView中,但没有从SVG图像的选定位置渲染数据。甚至我被搜索到更多关于这个功能的信息,我也没有得到任何关于这个功能的信息。同样,我也尝试过SVGKit,但我不了解SVGKit的完整场景,所以我选择UIWebView的原因。如果有人知道这个功能,请在这里发表评论 SVG图如下图所示。我发现很少有关于SVG图像的链接,但这些链接不是由UIWebView加载的 //创建一个空的test.html并覆盖下面的html代码,然后重新加载Webview。 -(UIWebV

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

SVG图如下图所示。我发现很少有关于SVG图像的链接,但这些链接不是由UIWebView加载的

//创建一个空的test.html并覆盖下面的html代码,然后重新加载Webview。
-(UIWebView*)设置{
self.svgName=@“第三”;
//创建网络视图
UIWebView*webview=[[UIWebView alloc]init];
//创建一个空的html文件,将svg转换为html并动态保存
NSString*testPath=[[NSBundle mainBundle]pathForResource:@“test”类型:@“html”inDirectory:nil];
NSString*filePath=[self-writeAndAppendString:[self-getHTMLStringForLoadSVG:[self.svgName stringByAppendingString:@.svg”]]toFile:testPath];
//将svg文件加载到webview中
//NSString*path=[[NSBundle mainBundle]pathForResource:@“loadSVG”类型:@“html”];
NSURL*fileURL=[[NSURL alloc]initFileURLWithPath:filePath];
NSURLRequest*req=[nsurlRequestRequestWithURL:fileURL];
[webview加载请求:req];
[webview setScalesPageToFit:是];
[webview setUserInteractionEnabled:否];
//webview.contentMode=UIViewContentModeScaleAspectFit;
[webview setBackgroundColor:[UIColor cyanColor]];
webview.delegate=self;
返回webview;
}
//HTML文件
-(NSString*)getHTMLStringForLoadSVG:(NSString*)svgFileName{
NSString*jQueryLib=@”https://code.jquery.com/jquery-3.2.1.min.js";
NSString*idOfJQuery=@“测试”;
NSString*type=@“image/svg+xml”;
NSString*id=@“测试”;
返回[NSString stringWithFormat:@”\n\n\n\n窗口。单击EDID;\n$(文档)。就绪(函数(){\nvar embed=document.getElementById('%@');\nembed.addEventListener('load',函数(){\nvar svg=embed.getSVGDocument();\nvar pathElements=svg.getElementsByTagName('path');\n(变量i=0;i)
//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)tapAction:(UITapGestureRecognizer *)sender{
        [self performSelector:@selector(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 *)writeAndAppendString:(NSString *)str toFile:(NSString *)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;
    }