使用iPhone打印和保存网页
我希望我的WebView保存为任何格式的图像或pdf 我尝试使用以下代码保存网页:使用iPhone打印和保存网页,iphone,ios,objective-c,ios5,ios4,Iphone,Ios,Objective C,Ios5,Ios4,我希望我的WebView保存为任何格式的图像或pdf 我尝试使用以下代码保存网页: UIGraphicsBeginImageContext(WebPage.frame.size); [WebPage.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImage
UIGraphicsBeginImageContext(WebPage.frame.size);
[WebPage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
但我得到的是看得见的部分
我想知道如何获得整个网页的图像,或者苹果公司的员工如何进行空气打印,以便能够打印整个网页。。我不想知道“AirPrint功能”,我想知道如何使用iPhone获取网页图像
因为我对iOS开发比较熟悉
有人能帮我保存网页的工作代码吗?要得到你的要求,你必须通过改变框架来改变
webview
的上下文,为此,取一个作为循环,并通过改变上下文来根据页数循环
要获得您的需求,您必须通过更改框架来更改网络视图的上下文
,为此,请选择一个作为循环,并通过更改上下文根据页数进行循环
单击按钮时调用此方法
- (void)printAndSave
{
webViewHeight = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];
CGRect screenRect = self.myWebView.frame;
double currentWebViewHeight = webViewHeight;
while (currentWebViewHeight > 0)
{
imageName ++;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.myWebView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];
if(currentWebViewHeight < 460)
{
CGRect lastImageRect = CGRectMake(0, 457 - currentWebViewHeight, self.myWebView.frame.size.width, currentWebViewHeight);
CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);
newImage = [UIImage imageWithCGImage:lastImageRef];
CGImageRelease(lastImageRef);
}
[UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,460);"];
currentWebViewHeight -= 460;
}
}
单击按钮时调用此方法-(无效)打印并保存
- (void)printAndSave
{
webViewHeight = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];
CGRect screenRect = self.myWebView.frame;
double currentWebViewHeight = webViewHeight;
while (currentWebViewHeight > 0)
{
imageName ++;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.myWebView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];
if(currentWebViewHeight < 460)
{
CGRect lastImageRect = CGRectMake(0, 457 - currentWebViewHeight, self.myWebView.frame.size.width, currentWebViewHeight);
CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);
newImage = [UIImage imageWithCGImage:lastImageRef];
CGImageRelease(lastImageRef);
}
[UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,460);"];
currentWebViewHeight -= 460;
}
}
{
webViewHeight=[[self.myWebView stringByEvaluatingJavaScriptFromString:@“document.body.scrollHeight;”“]integerValue];
CGRect screenRect=self.myWebView.frame;
double currentWebViewHeight=webViewHeight;
而(currentWebViewHeight>0)
{
imageName++;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx=UIGraphicsGetCurrentContext();
[[UIColor blackColor]set];
CGContextFillRect(ctx,screenRect);
[self.myWebView.layer renderInContext:ctx];
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*pngPath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@“%d.png”,imageName]];
如果(currentWebViewHeight<460)
{
CGRect lastImageRect=CGRectMake(0457-currentWebViewHeight,self.myWebView.frame.size.width,currentWebViewHeight);
CGImageRef lastImageRef=CGImageCreateWithImageInRect([newImage CGImage],lastImageRect);
newImage=[UIImage imageWithCGImage:lastImageRef];
CGImageRelease(lastImageRef);
}
[UIImagePngPresentation(newImage)WriteFile:pngPath原子性:是];
[self.myWebView stringByEvaluatingJavaScriptFromString:@“window.scrollBy(0460);”;
currentWebViewHeight-=460;
}
}
-(无效)打印并保存
{
webViewHeight=[[self.myWebView stringByEvaluatingJavaScriptFromString:@“document.body.scrollHeight;”“]integerValue];
CGRect screenRect=self.myWebView.frame;
double currentWebViewHeight=webViewHeight;
而(currentWebViewHeight>0)
{
imageName++;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx=UIGraphicsGetCurrentContext();
[[UIColor blackColor]set];
CGContextFillRect(ctx,screenRect);
[self.myWebView.layer renderInContext:ctx];
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*pngPath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@“%d.png”,imageName]];
如果(currentWebViewHeight<460)
{
CGRect lastImageRect=CGRectMake(0457-currentWebViewHeight,self.myWebView.frame.size.width,currentWebViewHeight);
CGImageRef lastImageRef=CGImageCreateWithImageInRect([newImage CGImage],lastImageRect);
newImage=[UIImage imageWithCGImage:lastImageRef];
CGImageRelease(lastImageRef);
}
[UIImagePngPresentation(newImage)WriteFile:pngPath原子性:是];
[self.myWebView stringByEvaluatingJavaScriptFromString:@“window.scrollBy(0460);”;
currentWebViewHeight-=460;
}
}
只要在您的主目录中看到png就会出现。。。。iphone Simulator-->iOS版本-->您的应用程序-->现在检查编辑的答案,然后运行并在Simulator或Device中查看您的相机相册。谢谢它帮助了我,但我能知道如何将此图像保存为部分吗?这意味着图像太长了。。我希望它将下一部分保存为另一个图像。。如何做到这一点?意味着你想要的确切方式?只要在你的主目录中看到png就会出现。。。。iphone Simulator-->iOS版本-->您的应用程序-->现在检查编辑的答案,然后运行并在Simulator或Device中查看您的相机相册。谢谢它帮助了我,但我能知道如何将此图像保存为部分吗?这意味着图像太长了。。我希望它将下一部分保存为另一个图像。。如何做到这一点?确切地说是指您想要的方式?谢谢:此文件中的代码对我有一点帮助。。要获得2部分的预览。。但是如何保存我没有得到的..谢谢:这个文件中的代码帮助了我一点。。要获得2部分的预览。。但是如何保存我没有得到的..我有一个疑问。。如果我向下滚动到第二页,它将从第二页开始保存。。只有第1页会有问题..你在哪里写这个方法?点击我正在写这个方法。。当我滚动到第二页。。它将保存当前页面n下一步所有页面都是正确的..保留在-(void)webViewDidFinishLoad:(UIWebView*)webView方法中,然后查看它在“-(void)WebViewDidStarLoad:(UIWebView*)webView”和“-(void)webViewDidFinishLoad:(UIWebView*)webView”中不起作用..我有一个疑问。。如果我向下滚动到第二页,它将从第二页开始保存。。只有第一页会显示