UIWebView十分适合冬日里让手机给取暖
创建UIWebView
获取页面的名称
webtitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
获取当前页面的ip地址
NSString *hostname = _domainStr;
调用此方法应是传入域名,当前页面url获取不到
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostname);
if (hostRef)
{
Boolean result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
if (result == TRUE)
{
NSArray *addresses = (__bridge NSArray*)CFHostGetAddressing(hostRef, &result);
tempDNS = [[NSMutableArray alloc] init];
for(int i = 0; i < addresses.count; i++)
{
struct sockaddr_in* remoteAddr;
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex((__bridge CFArrayRef)addresses, i);
remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);
if(remoteAddr != NULL)
{
const char *strIP41 = inet_ntoa(remoteAddr->sin_addr);
NSString *strDNS =[NSString stringWithCString:strIP41 encoding:NSASCIIStringEncoding];
// NSLog(@"RESOLVED %d:<%@>", i, strDNS);
[tempDNS addObject:strDNS];
}}}}
修改uesr-agent 电脑网页与手机网页的切换
NSDictionary*dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
修改UA浏览电脑网页。此方法需在loadRequest前调用 否则无效
NSDictionary*dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69", @"UserAgent", nil];[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
修改UA浏览手机网页。此方法需在loadRequest前调用 否则无效
对webview全页面进行抓取,主要是对webview中scrollview的抓取
首先在抓取页面时,先将webView.scrollView置为初始。setScalesPageToFit = yes之前设置了自适应
[webView.scrollView setZoomScale:self.webView.scrollView.minimumZoomScale animated:YES];
[webView.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
对webview页面进行绘制图片
- (UIImage *)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets {
CGFloat scale = [UIScreen mainScreen].scale;
CGSize boundsSize = self.webView.bounds.size;
CGFloat boundsHeight = boundsSize.height;
CGSize contentSize = self.webView.scrollView.contentSize;
CGFloat contentHeight = contentSize.height;
CGFloat contentWidth = contentSize.width;
CGPoint offset = self.webView.scrollView.contentOffset;
[self.webView.scrollView setContentOffset:CGPointMake(0, 0)];
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
preMemory = [self usedMemory];//监控内存
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, [UIScreen mainScreen].scale);
[self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[images addObject:image];
CGFloat offsetY = self.webView.scrollView.contentOffset.y;
[self.webView.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
[self.webView.scrollView setContentOffset:offset];
CGSize imageSize = CGSizeMake(contentSize.width * scale,contentSize.height * scale);
UIGraphicsBeginImageContext(imageSize);
[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
[image drawInRect:CGRectMake(0,scale * boundsHeight * idx,scale * boundsWidth,scale * boundsHeight)];}];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * snapshotView = [[UIImageView alloc]initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
snapshotView.image = [fullImage resizableImageWithCapInsets:capInsets];
[self handleActivityStop];
return snapshotView.image;
}网页过大会引起内存爆炸,生成的图片在40M以内没问题
网友评论