+++
Categories = ["iOS",]
Tags = ["iOS","snapshot",]
date = "2014-12-22T11:22:30+08:00"
title = "iOS 截屏拼图"
+++
1. 普通界面
ios 7 以后截图
ios7中添加了调用 snapshotViewAfterScreenUpdates
创建一个复合视图的快照。然后返回一个uiview
对象来表示调用视图的整体外观。
Supplying YES for -snapshotViewAfterScreenUpdates: means it needs a trip back to the runloop to actually draw the image. If you supply NO, it will try immediately, but if your view is off screen or otherwise hasn't yet drawn to the screen, the snapshot will be empty.
因为返回的是一个view对象,所以,你可以更改它以及它的layer属性.但是呢,你不能够修改它的layer的content属性;如果你试图这么做,将不会有任何效果.如果当前的view还没有渲染,或者这么说吧,因为还没有出现在屏幕上,那么,这个截取的view将不会有能显示的content.
如果你想要加载一个图形效果,比如blur,请使用这个方法 drawViewHierarchyInRect:afterScreenUpdates:
来代替.
#import "UIImage+ImageEffects.h"
- (void)createBlurredSnapshot {
UIGraphicsBeginImageContextWithOptions(self.targetImageView.bounds.size, NO, 0);
BOOL result = [self.sourceImageView drawViewHierarchyInRect:self.targetImageView.bounds afterScreenUpdates:YES];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (result)
self.targetImageView.image = [snapshotImage applyLightEffect];
else
NSLog(@"drawViewHierarchyInRect failed");
}
iOS7 以前截图
- (UIImage *)captureCurrentView:(UIView *)view {
CGRect frame = view.frame;
UIGraphicsBeginImageContext(frame.size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
[view.layer renderInContext:contextRef];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
// 获取指定区域的截图
CGImageRef imageRef = image.CGImage;
CGRect rect = CGRectMake(0, 0, 300, 300); //设置指定区域
CGImageRef editImageRef = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *editImage = [UIImage alloc] initWithCGImage:editImageRef];
return editImage;
}
2. UIScorllView 截图
- (void)screenShot{
UIImage *image = nil;
UIGraphicsBeginImageContext(_scrollView.contentSize);
{
CGPoint savedContentOffset = _scrollView.contentOffset;
CGRect savedFrame = _scrollView.frame;
_scrollView.contentOffset = CGPointZero;
_scrollView.frame = CGRectMake(0, 0, m_scrollView.contentSize.width, m_scrollView.contentSize.height);
[_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
_scrollView.contentOffset = savedContentOffset;
_scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
if (image != nil) {
NSLog(@"success snapshot!");
}
}
3. 截图后拼图
-(void)combinImg:(id)sender
{
if (self.imgArr.count <=0 ) {
return;
}
NSString *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
CGFloat tatolHight = self.tableView.contentSize.height + self.tableView.contentInset.top;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.tableView.frame.size.width, tatolHight), NO, 1);
CGFloat orgy = 0;
for(int i = 0; i < self.imgArr.count;i++)
{
UIImage *image = (UIImage*)self.imgArr[i];
[image drawInRect:CGRectMake(0, orgy,image.size.width, image.size.height)];
orgy += image.size.height;
}
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.tableView.frame.size.height )];
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.tableView.contentSize.height + 64);
scrollView.backgroundColor = [UIColor brownColor];
[self.view addSubview:scrollView];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, scrollView.frame.size.width, self.tableView.contentSize.height)];
imgView.backgroundColor = [UIColor purpleColor];
imgView.image = img;
[scrollView addSubview:imgView];
//写到文件中可打开文件查看
NSString *path = [rootPath stringByAppendingPathComponent:@"combin.png"];
NSData *imgData = UIImagePNGRepresentation(img);
[imgData writeToFile:path atomically:YES];
}