微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

要将HTML doc转换为可可中的图像

如何解决要将HTML doc转换为可可中的图像

| 是否可以将HTML页面转换为可可图像? 实际上,我已经在HTML中创建了完整视图,现在我想将整个html预览转换为图像(任何jpeg或png等)。 我在网络上找不到任何资源或示例,这可以为我上面的查询提供某种帮助。如果有人可以分享我的智慧,我将非常感激。 提前致谢..     

解决方法

首先,我要感谢塞尔吉奥...他的回答让我开始了,但是我认为我会分享一些我认为没有明显的代码才能使它起作用的代码: 这是在不显示页面的情况下为其制作缩略图的方法:
// Your width and height can be whatever you like,but if you want this to render
// off screen,you need an x and y bigger than the superview\'s width and height
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(largerScreenDimension,largerScreenDimension,largerScreenDimension)];
[self.view addSubview:webView]; // UIWebViews without an assigned superview don\'t load ever.
webView.delegate = self; // or whoever you have implement UIWebViewDelegate
webView.scalesToFit = YES; // This zooms the page appropriately to fill the entire thumbnail.
[webView loadRequest:[NSURLRequest requestWithURL:url]];
然后在您的委托中实现:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    UIGraphicsBeginImageContext(webView.bounds.size);
    [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *webViewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *thumbnailData = UIImagePNGRepresentation(webViewImage);
    [webView removeFromSuperview];
}
最后,要显示此缩略图,您将需要以下内容:
thumbnailImageView.image = [UIImage imageWithData:thumbnailData];
值得一提的是,我希望一次生成多个缩略图。我发现使用
objc_setAssociatedObject()
objc_getAssociatedObject()
对跟踪哪个webView正在加载哪个缩略图非常有帮助。但是,详细说明其工作方式超出了此问题的范围。     ,您可以在图像上下文中绘制视图,如下所示:
UIWebView* view = ...
....
UIGraphicsBeginImageContext(view.bounds.size);    
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imagedata = UIImagePNGRepresentation(viewimage);
NSString *encodedString = [imageData base64Encoding];
另一个选择是使用Quartz PDF引擎创建PDF。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。