iOS 截图
UIView截图
- (UIImage *)captureView:(UIView *)theView { CGRect rect = theView.frame; if ([theView isKindOfClass:[UIScrollView class]]) { rect.size = ((UIScrollView *)theView).contentSize; } UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [theView.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; }
UIScrollview截图
1 - (UIImage *)captureScrollView:(UIScrollView *)scrollView 2 { 3 UIImage* image = nil; 4 UIGraphicsBeginImageContext(scrollView.contentSize); 5 { 6 CGPoint savedContentOffset = scrollView.contentOffset; 7 CGRect savedFrame = scrollView.frame; 8 scrollView.contentOffset = CGPointZero; 9 scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height); 10 11 [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()]; 12 image = UIGraphicsGetImageFromCurrentImageContext(); 13 14 scrollView.contentOffset = savedContentOffset; 15 scrollView.frame = savedFrame; 16 } 17 UIGraphicsEndImageContext(); 18 19 if (image != nil) { 20 return image; 21 } 22 return nil; 23 }
合并多张图片
1 - (UIImage *)composeWithHeader:(UIImage *)header content:(UIImage *)content footer:(UIImage *)footer 2 { 3 CGSize size = CGSizeMake(content.size.width, header.size.height +content.size.height +footer.size.height); 4 UIGraphicsBeginImageContext(size); 5 [header drawInRect:CGRectMake(0, 6 0, 7 header.size.width, 8 header.size.height)]; 9 [content drawInRect:CGRectMake(0, 10 header.size.height, 11 content.size.width, 12 content.size.height)]; 13 [footer drawInRect:CGRectMake(0, 14 header.size.height+content.size.height, 15 footer.size.width, 16 footer.size.height)]; 17 18 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 19 UIGraphicsEndImageContext(); 20 return image; 21 }