UIWebView全部区域截图保存为UIImage并保存到相册

Image

//获取图片
- (UIImage *)imageRepresentation{
    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize boundsSize = self.webView.bounds.size;
    CGFloat boundsWidth = boundsSize.width;
    CGFloat boundsHeight = boundsSize.height;
    CGSize contentSize = self.webView.scrollView.contentSize;
    CGFloat contentHeight = contentSize.height;
    CGPoint offset = self.webView.scrollView.contentOffset;
    
    [self.webView.scrollView setContentOffset:CGPointMake(0, 0)];
    
    NSMutableArray *images = [NSMutableArray array];
    while (contentHeight > 0) {
        UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);
        [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();
    return fullImage;
}
//保存图片到相册
-(void)saveImageToPhotos:(UIImage *)image{
    
    UIImageWriteToSavedPhotosAlbum(image, self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);
    
}
//保存回调
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo

{
    
    NSString *msg = nil ;
    
    if(error != NULL){
        
        msg =@"保存图片失败" ;
        
    }else{
        
        msg = @"保存图片成功" ;
    }
}

PDF(没有验证是否可行)

- (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;  
}  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容