zoukankan      html  css  js  c++  java
  • UIWebView全部区域截图保存为UIImage或者PDF

    //  
    //  UIWebView+ToFile.h  
    //  UIWebViewToFile  
    //  
    //  Created by Tracy E on 13-6-10.  
    //  Copyright (c) 2013 EsoftMobile.com. All rights reserved.  
    //  
      
    #import <UIKit/UIKit.h>  
      
    @interface UIWebView (ToFile)  
      
    - (UIImage *)imageRepresentation;  
      
    - (NSData *)PDFData;  
      
    @end  
    //  
    //  UIWebView+ToFile.m  
    //  UIWebViewToFile  
    //  
    //  Created by Tracy E on 13-6-10.  
    //  Copyright (c) 2013 EsoftMobile.com. All rights reserved.  
    //  
      
    #import "UIWebView+ToFile.h"  
    #import <QuartzCore/QuartzCore.h>  
      
    @implementation UIWebView (ToFile)  
      
    - (UIImage *)imageRepresentation{  
        CGFloat scale = [UIScreen mainScreen].scale;  
          
        CGSize boundsSize = self.bounds.size;  
        CGFloat boundsWidth = boundsSize.width;  
        CGFloat boundsHeight = boundsSize.height;  
          
        CGSize contentSize = self.scrollView.contentSize;  
        CGFloat contentHeight = contentSize.height;  
      
        CGPoint offset = self.scrollView.contentOffset;  
      
        [self.scrollView setContentOffset:CGPointMake(0, 0)];  
          
        NSMutableArray *images = [NSMutableArray array];  
        while (contentHeight > 0) {  
            UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);  
            [self.layer renderInContext:UIGraphicsGetCurrentContext()];  
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
            UIGraphicsEndImageContext();  
            [images addObject:image];  
              
            CGFloat offsetY = self.scrollView.contentOffset.y;  
            [self.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];  
            contentHeight -= boundsHeight;  
        }  
        [self.scrollView setContentOffset:offset];  
          
        CGSize imageSize = CGSizeMake(contentSize.width * scale,  
                                      contentSize.height * scale);  
        UIGraphicsBeginImageContext(imageSize);  
        [images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOLBOOL *stop) {  
            [image drawInRect:CGRectMake(0,  
                                         scale * boundsHeight * idx,  
                                         scale * boundsWidth,  
                                         scale * boundsHeight)];  
        }];  
        UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();  
        UIGraphicsEndImageContext();  
        return fullImage;  
    }  
      
    - (NSData *)PDFData{  
        UIViewPrintFormatter *fmt = [self viewPrintFormatter];  
        UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];  
        [render addPrintFormatter:fmt startingAtPageAtIndex:0];  
        CGRect page;  
        page.origin.x=0;  
        page.origin.y=0;  
        page.size.width=600;  
        page.size.height=768;  
          
        CGRect printable=CGRectInset( page, 50, 50 );  
        [render setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];  
        [render setValue:[NSValue valueWithCGRect:printable] forKey:@"printableRect"];  
          
        NSMutableData * pdfData = [NSMutableData data];  
        UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );  
          
        for (NSInteger i=0; i < [render numberOfPages]; i++)  
        {  
            UIGraphicsBeginPDFPage();  
            CGRect bounds = UIGraphicsGetPDFContextBounds();  
            [render drawPageAtIndex:i inRect:bounds];  
              
        }  
        UIGraphicsEndPDFContext();  
        return pdfData;  
    }  
      
      
    @end  
  • 相关阅读:
    Android网页打开指定App
    使用Android Studio Gradle实现友盟多渠道打包
    Android开发 PopupWindow弹窗调用第三方地图(百度,高德)实现导航功能
    Android使用Mob ShareSDK 分享不同平台
    Android布局优化之层级优化
    (Facebook开源项目)Fresco:一个新的Android图像处理类库
    关于Android开发的几点建议
    [AndroidTips]startService与bindService的区别
    基于HBase的手机数据备份系统 .
    MySQL在CenterOS和Ubuntu的安装
  • 原文地址:https://www.cnblogs.com/mkai/p/6635759.html
Copyright © 2011-2022 走看看