【问题标题】:In Obj C, How to add SVG blueprint image and rendering the data from UIWebView?在 Obj C 中,如何添加 SVG 蓝图图像并从 UIWebView 渲染数据?
【发布时间】:2017-10-05 10:33:06
【问题描述】:

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

SVG 地图如下图所示。我发现很少有关于 SVG 图像的链接,但 UIWebView 没有加载这些链接。 SVGKIT & chensheng12330/SVGKitDemo

【问题讨论】:

    标签: javascript ios objective-c svg uiwebview


    【解决方案1】:
    //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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 2018-01-29
      • 2011-09-30
      • 2020-03-23
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多