Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从UIWebView或UIView获取PDF/PNG作为输出_Iphone_Objective C_Cocoa Touch_Pdf_Png - Fatal编程技术网

Iphone 从UIWebView或UIView获取PDF/PNG作为输出

Iphone 从UIWebView或UIView获取PDF/PNG作为输出,iphone,objective-c,cocoa-touch,pdf,png,Iphone,Objective C,Cocoa Touch,Pdf,Png,有没有办法获取UIWebView的内容并将其转换为PDF或PNG文件?例如,当从Safari打印时,我想通过选择PDF按钮获得与Mac上类似的输出。我假设这是不可能的/内置的,但希望我会感到惊讶,并找到一种方法将内容从webview获取到文件 谢谢 从web视图创建图像很简单: UIImage* image = nil; UIGraphicsBeginImageContext(offscreenWebView_.frame.size); { [offscreenWebView_.lay

有没有办法获取
UIWebView
的内容并将其转换为PDF或PNG文件?例如,当从Safari打印时,我想通过选择PDF按钮获得与Mac上类似的输出。我假设这是不可能的/内置的,但希望我会感到惊讶,并找到一种方法将内容从webview获取到文件


谢谢

从web视图创建图像很简单:

UIImage* image = nil;

UIGraphicsBeginImageContext(offscreenWebView_.frame.size);
{
    [offscreenWebView_.layer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
获得图像后,可以将其保存为PNG


也可以通过非常类似的方式创建PDF,但仅限于尚未发布的iPhone OS版本。

您可以在UIView上使用以下类别创建PDF文件:

#import <QuartzCore/QuartzCore.h>

@implementation UIView(PDFWritingAdditions)

- (void)renderInPDFFile:(NSString*)path
{
    CGRect mediaBox = self.bounds;
    CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], &mediaBox, NULL);

    CGPDFContextBeginPage(ctx, NULL);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
    [self.layer renderInContext:ctx];
    CGPDFContextEndPage(ctx);
    CFRelease(ctx);
}

@end
#导入
@实现UIView(PDFWritingAdditions)
-(void)renderUpffile:(NSString*)路径
{
CGRect mediaBox=self.bounds;
CGContextRef ctx=CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],&mediaBox,NULL);
CGPDFContextBeginPage(ctx,NULL);
CGContextScaleCTM(ctx,1,-1);
CGContextTranslateCTM(ctx,0,-mediaBox.size.height);
[self.layer renderInContext:ctx];
CGPDFContextEndPage(ctx);
CFX释放(ctx);
}
@结束

坏消息:UIWebView不会在PDF中创建漂亮的形状和文本,但会将自身作为图像呈现到PDF中。

@mjdth,请尝试
fileURLWithPath:isDirectory:
URLWithString
也不适合我

@implementation UIView(PDFWritingAdditions)

- (void)renderInPDFFile:(NSString*)path
{
    CGRect mediaBox = self.bounds;
    CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path isDirectory:NO], &mediaBox, NULL);

    CGPDFContextBeginPage(ctx, NULL);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
    [self.layer renderInContext:ctx];
    CGPDFContextEndPage(ctx);
    CFRelease(ctx);
}

@end

下面的代码将UIWebView的(完整)内容转换为UIImage

渲染UIImage后,我将其作为PNG写入磁盘以查看结果。
当然,你可以随心所欲地使用UIImage

UIImage *image = nil;
CGRect oldFrame = webView.frame;

// Resize the UIWebView, contentSize could be > visible size
[webView sizeToFit];
CGSize fullSize = webView.scrollView.contentSize;

// Render the layer content onto the image  
UIGraphicsBeginImageContext(fullSize);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
[webView.layer renderInContext:resizedContext];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Revert the UIWebView back to its old size
webView.frame = oldFrame;

// Write the UIImage to disk as PNG so that we can see the result
NSString *path= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

注意:确保UIWebView已完全加载(UIWebViewDelegate或loading属性)。

我认为CGPDF。。。API从iPhoneOS2.0开始就存在了。是的,但我认为将层渲染到PDF上下文是一种新的东西。这个答案几乎也适用。问题是页面大小在页面之间发生变化。有没有办法确定webview中内容的大小,以便我可以创建一个大小正确的上下文?@St3fan如果我们想在UIWebView中呈现内容,而UIWebView由于内容不适合框架而可滚动,上述回答只呈现UIWebView框架中可见的内容。我尝试过为CGRect mediaBox提供一个大的假设框架=大的假设框架;但结果仍然是一样的。只有在webView框架中可见的内容才会呈现在pdf中。我们可以用任何方式在pdf中呈现整个webView内容(即使是不可见但在webView中加载的内容?@mjdth如果我们想在UIWebView中呈现内容,而UIWebView由于内容不适合框架而可滚动,上述回答只呈现UIWebView框架中可见的内容。我尝试过为CGRect mediaBox提供一个大的假设框架=大的假设框架;但结果仍然是一样的。只有在webView框架中可见的内容才会呈现在pdf中。我们可以用什么方式在pdf中呈现整个webView内容(即使是不可见但在webView中加载的内容)?谢谢你的帖子。我遇到了一个问题,CGPDFContextCreateWithURL没有正确创建上下文。我尝试了几个不同的URL,但似乎没有任何改变。有什么想法吗?CFURLRef的创建过程中有一个bug。我修复了它,现在应该可以用于正确的文件URL。使用此方法呈现UILabel似乎可以光栅化文本。如果您希望文本保持矢量化,请按此处所述将其直接绘制到图层中。有人能告诉我如何访问在“我的viewController”的“类别”中定义的此方法吗?因为这个类别对我们来说是全新的me@NikolaiRuhe如果我们想在UIWebView中呈现内容,而UIWebView是可滚动的,因为内容不适合框架内,那么上述回答只呈现UIWebView框架内可见的内容。我尝试过为CGRect mediaBox提供一个大的假设框架=大的假设框架;但结果仍然是一样的。只有在webView框架中可见的内容才会呈现在pdf中。我们是否可以在pdf中呈现整个webView内容(即使是不可见但在webView中加载的内容)?