UIWebView十分適合冬日里讓手機(jī)給取暖
創(chuàng)建UIWebView?
獲取頁面的名稱
webtitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
獲取當(dāng)前頁面的ip地址
NSString *hostname = _domainStr;
調(diào)用此方法應(yīng)是傳入域名,當(dāng)前頁面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 ?電腦網(wǎng)頁與手機(jī)網(wǎng)頁的切換
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瀏覽電腦網(wǎng)頁。此方法需在loadRequest前調(diào)用 否則無效
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瀏覽手機(jī)網(wǎng)頁。此方法需在loadRequest前調(diào)用 否則無效
對webview全頁面進(jìn)行抓取,主要是對webview中scrollview的抓取
首先在抓取頁面時,先將webView.scrollView置為初始。setScalesPageToFit = yes之前設(shè)置了自適應(yīng)
[webView.scrollView setZoomScale:self.webView.scrollView.minimumZoomScale animated:YES];
[webView.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
對webview頁面進(jìn)行繪制圖片
- (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];//監(jiān)控內(nèi)存
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;
}網(wǎng)頁過大會引起內(nèi)存爆炸,生成的圖片在40M以內(nèi)沒問題