这是调用
//用来截取测试的view
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
testView.backgroundColor = [UIColor redColor];
self.shotImage = [[UIImage alloc] init];
_shotImage = [self viewSnapshot:testView withInRect:testView.bounds];
UIImageWriteToSavedPhotosAlbum(_shotImage, nil, nil, nil); //将截图存入相册
截图方法
- (UIImage *)viewSnapshot:(UIView *)view withInRect:(CGRect)rect;
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
image = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage,rect)];
return image;
}
如果想实现截取整个手机屏幕,包括状态栏,那么以上方法是实现不了的,我们可以使用一下的方法
//截取整个屏幕
-(void)fullScreenshots{
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIWindow *screenWindow = appDelegate.window;
UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
_shotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(_shotImage, nil, nil, nil); //将截图存入相册
NSLog(@"%@",_shotImage);
}