UITableView或UIScrollView的content截屏

来源:互联网 发布:试点网络学校基础 编辑:程序博客网 时间:2024/04/29 19:17

我们都知道,给手机屏幕做截图很容易,如下面代码


[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (UIImage*) imageWithUIView:(UIView*) view{  
  2.     // 创建一个bitmap的context  
  3.     // 并把它设置成为当前正在使用的context  
  4.     UIGraphicsBeginImageContext(view.bounds.size);  
  5.     CGContextRef currnetContext = UIGraphicsGetCurrentContext();  
  6.     //[view.layer drawInContext:currnetContext];  
  7.     [view.layer renderInContext:currnetContext];  
  8.     // 从当前context中创建一个改变大小后的图片  
  9.     UIImage* image = UIGraphicsGetImageFromCurrentImageContext();  
  10.     // 使当前的context出堆栈  
  11.     UIGraphicsEndImageContext();  
  12.     return image;  
  13. }  

那很多聪明同学就发现,如果对tableView 截图,不就是改变一下里的参数不就行了吗?

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UIGraphicsBeginImageContext(tableView.<span style="color:#ff6666;">contentSize</span>)  

我也犯过这样的错误,也曾经天真的以为就是这么easy


那到底如何给UITableView 或 UIScrollView 的content 做截图


1. 给UITableView ,UIScrollView 添加category,让其能顺利的拿到tableView 的一些属性如

numberOfSections,

numberOfRowsInSection


2. 建立一个存放UIImage 的数组screenshots,然后开始对局部进行截图,代码如下

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (UIImage *)screenshotExcludingHeadersAtSections:(NSSet *)excludedHeaderSections  
  2.                        excludingFootersAtSections:(NSSet *)excludedFooterSections  
  3.                         excludingRowsAtIndexPaths:(NSSet *)excludedIndexPaths  
  4. {  
  5.     NSMutableArray *screenshots = [NSMutableArray array];  
  6.     // Header Screenshot  
  7.     UIImage *headerScreenshot = [self screenshotOfHeaderView];  
  8.     if (headerScreenshot) [screenshots addObject:headerScreenshot];  
  9.     for (int section=0; section<self.numberOfSections; section++) {  
  10.         // Header Screenshot  
  11.         UIImage *headerScreenshot = [self screenshotOfHeaderViewAtSection:section excludedHeaderSections:excludedHeaderSections];  
  12.         if (headerScreenshot) [screenshots addObject:headerScreenshot];  
  13.           
  14.         // Screenshot of every cell of this section  
  15.         for (int row=0; row<[self numberOfRowsInSection:section]; row++) {  
  16.             NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:row inSection:section];  
  17.             UIImage *cellScreenshot = [self screenshotOfCellAtIndexPath:cellIndexPath excludedIndexPaths:excludedIndexPaths];  
  18.             if (cellScreenshot) [screenshots addObject:cellScreenshot];  
  19.         }  
  20.           
  21.         // Footer Screenshot  
  22.         UIImage *footerScreenshot = [self screenshotOfFooterViewAtSection:section excludedFooterSections:excludedFooterSections];  
  23.         if (footerScreenshot) [screenshots addObject:footerScreenshot];  
  24.     }  
  25.     UIImage *footerScreenshot = [self screenshotOfFooterView];  
  26.     if (footerScreenshot) [screenshots addObject:footerScreenshot];  
  27.     return [UIImage <span style="color:#ff6666;">verticalImageFromArray:screenshots</span>];  
  28. }  


3. 上面代码中你可能已经看到了,把所有小图拼接成一张大图,思想是先计算所有图加起来的高度,然后便利每张小图用drawPoint 发放在计算起来高度的context上画一张新图


[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @implementation UIImage (DHImageFromArrayUtils)  
  2.   
  3. + (UIImage *)verticalImageFromArray:(NSArray *)imagesArray  
  4. {  
  5.     UIImage *unifiedImage = nil;  
  6.     CGSize totalImageSize = [self verticalAppendedTotalImageSizeFromImagesArray:imagesArray];  
  7.     UIGraphicsBeginImageContextWithOptions(totalImageSize, NO, 0.f);  
  8.     // For each image found in the array, create a new big image vertically  
  9.     int imageOffsetFactor = 0;  
  10.     for (UIImage *img in imagesArray) {  
  11.         [img <span style="color:#ff6666;">drawAtPoint:CGPointMake</span>(0, imageOffsetFactor)];  
  12.         imageOffsetFactor += img.size.height;  
  13.     }  
  14.       
  15.     unifiedImage = UIGraphicsGetImageFromCurrentImageContext();  
  16.     UIGraphicsEndImageContext();  
  17.     return unifiedImage;  
  18. }  
  19.   
  20. + (CGSize)verticalAppendedTotalImageSizeFromImagesArray:(NSArray *)imagesArray  
  21. {  
  22.     CGSize totalSize = CGSizeZero;  
  23.     for (UIImage *im in imagesArray) {  
  24.         CGSize imSize = [im size];  
  25.         totalSize.height += imSize.height;  
  26.         // The total width is gonna be always the wider found on the array  
  27.         totalSize.width = MAX(totalSize.width, imSize.width);  
  28.     }  
  29.     return totalSize;  
  30. }  


那到现在是不是有点明白了,对,就是这么简单,利用tableView scrollRectToVisible 的特性,先在各个小区域截图,存放数组,然后再遍历数组计算小图累计的高度,然后利用drawPoint 重新画大图。

0 0
原创粉丝点击