这个功能就是对网页的存储,存储成png格式的图片 且不失真 很棒的一个小方法。具体实现如下:
- (void)webViewDidFinishLoad:(UIWebView *)webView1 { [self.view addSubview:webView1]; [self performSelector:@selector(photo) withObject:webView afterDelay:1];//webview快照 } -(void)photo { UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, FALSE, 0); //渲染图形 // [[webView.scrollView layer] renderInContext:UIGraphicsGetCurrentContext()];//这个方法没下边的好 因为快照截图 只截了大部分 还剩一点没截到 [[[webView.scrollView.subviews objectAtIndex:0] layer] renderInContext:UIGraphicsGetCurrentContext()];//这个subview 的名字是 UIWebBrowserView //得到新的image UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); NSLog(@"%@",image); //关闭context UIGraphicsEndImageContext(); NSString *filepath=[Function achieveThe_filepath:@"test.png"];//这个方法就是读取文件路径 和写文件的类型 png 读取文件路径方法在上一节随笔
//这里的命名应该优化 成获取系统时间 也就是命名形式应该加上时间戳前缀 这样做一个唯一标识更加富有逻辑性 NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)]; [data1 writeToFile:filepath atomically:YES]; }
快照结果上图片 一点也不失真 是不是