渲染包含 SVG 文件的 UIWebView 图层

发布于 2024-10-20 04:02:23 字数 1280 浏览 2 评论 0原文

我正在尝试使用我构建的这个方法将 UIWebView 的图层保存为图像。这是可行的,但是我加载到 UIView 中的 SVG 没有在图像中渲染,只有 UIWebView 的背景。

+ (BOOL)imageFromSVGinWebView:(UIView *)theView usePNG:(BOOL)usePNG; {  
    UIGraphicsBeginImageContext(theView.frame.size);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *layerIMGData = nil;
    NSString *pathComponent = nil;
    if (usePNG) {
        layerIMGData = UIImagePNGRepresentation(layerImage);
        pathComponent = @"png";
    }else {
        layerIMGData = UIImageJPEGRepresentation(layerImage,1.0f);
        pathComponent = @"jpg";
    }
    NSString *appFile = [[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:String(@"img.%@",pathComponent)];
    NSError *error = nil;
    BOOL returnBOOL = [layerIMGData writeToFile:appFile options:NSDataWritingAtomic error:&error];
    if(error != nil)
        ILogPlus(@"%@",error);
    return returnBOOL;
}

总之,我已将 SVG 加载到 UIWebView 中,将该 UIWebView 保存为图像,打开该图像,但该图像中没有 SVG。我已经成功地对其他视图完成了此操作,但 SVG 或 UIWebView 有所不同。

更新: 如果我添加延迟以使应用程序等待几秒钟来执行图像保存代码,那么它就可以工作。将该代码放入 webViewDidFinishLoad 中不起作用。显然 webViewDidFinishLoad 不计算 SVG。

I am attempting to use this method I've constructed to save the layer of a UIWebView as an image. That is working but the SVG I have loaded into the UIView is not getting rendered in the image, only the background of the UIWebView.

+ (BOOL)imageFromSVGinWebView:(UIView *)theView usePNG:(BOOL)usePNG; {  
    UIGraphicsBeginImageContext(theView.frame.size);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *layerIMGData = nil;
    NSString *pathComponent = nil;
    if (usePNG) {
        layerIMGData = UIImagePNGRepresentation(layerImage);
        pathComponent = @"png";
    }else {
        layerIMGData = UIImageJPEGRepresentation(layerImage,1.0f);
        pathComponent = @"jpg";
    }
    NSString *appFile = [[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:String(@"img.%@",pathComponent)];
    NSError *error = nil;
    BOOL returnBOOL = [layerIMGData writeToFile:appFile options:NSDataWritingAtomic error:&error];
    if(error != nil)
        ILogPlus(@"%@",error);
    return returnBOOL;
}

In summary, I've loaded an SVG into a UIWebView, saved that UIWebView as an image, opened the image and the image does not have the SVG in it. I have done this with other views successfully, something is different about the SVG or UIWebView.

UPDATE:
If I add a delay to make the app wait a few seconds to execute the image saving code then it works. Putting that code in webViewDidFinishLoad does NOT work. Obviously webViewDidFinishLoad doesn't count SVGs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2024-10-27 04:02:23

我发现如果你添加 0.1 秒的延迟,它就会起作用

- (void)webViewDidFinishLoad:(UIWebView *)webView; {
    [self performSelector:@selector(postLoadActions:) withObject:webView afterDelay:0.1f];
}


- (void)postLoadActions:(UIWebView *)webView; {
    if (![CCConversion imageFromSVGinWebView:webView usePNG:YES]) {
        ILogPlus(@"Didn't work");
    }
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[CCFileManagement conversionsDirectory] error:NULL];
    ILogPlus(@"%@",array);
    //[webView removeFromSuperview];
    [webView release];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:@"img.png"]]];
    imageView.frame = CGRectMake(90, 90, imageView.frame.size.width, imageView.frame.size.height);
    imageView.backgroundColor = [UIColor clearColor];
    [self.view performSelector:@selector(addSubview:) withObject:imageView afterDelay:2.0f];
    [imageView release];
}

I have found that if you add a .1 second delay it will work

- (void)webViewDidFinishLoad:(UIWebView *)webView; {
    [self performSelector:@selector(postLoadActions:) withObject:webView afterDelay:0.1f];
}


- (void)postLoadActions:(UIWebView *)webView; {
    if (![CCConversion imageFromSVGinWebView:webView usePNG:YES]) {
        ILogPlus(@"Didn't work");
    }
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[CCFileManagement conversionsDirectory] error:NULL];
    ILogPlus(@"%@",array);
    //[webView removeFromSuperview];
    [webView release];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:@"img.png"]]];
    imageView.frame = CGRectMake(90, 90, imageView.frame.size.width, imageView.frame.size.height);
    imageView.backgroundColor = [UIColor clearColor];
    [self.view performSelector:@selector(addSubview:) withObject:imageView afterDelay:2.0f];
    [imageView release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文