官方给的该方法的说明
/** Rendering properties and methods. **/
/* Renders the receiver and its sublayers into 'ctx'. This method
* renders directly from the layer tree. Renders in the coordinate space
* of the layer.
* WARNING: currently this method does not implement the full
* CoreAnimation composition model, use with caution. */
- (void)renderInContext:(CGContextRef)ctx;
渲染一个图层和其子图层到 'ctx' ; 注意上面的WARNING: not implement the full CoreAnimation composition model
//renderInContext:使用此函数实现截图,在有核心动画效果的视图上有差错: not implement the full CoreAnimation composition model
- (UIImage*)screenShotFromView:(UIView*)mainView
{
CGFloatscale = [UIScreenmainScreen].scale;
CGSizeshotSize =CGSizeMake(mainView.bounds.size.width*scale, mainView.bounds.size.height*scale);
UIGraphicsBeginImageContextWithOptions(shotSize,YES,0);
//设置截屏大小
[mainView.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage*viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRefimageRef = viewImage.CGImage;
CGRectrect =CGRectMake(0,0, shotSize.width, shotSize.height);//这里可以设置想要截图的区域
CGImageRefimageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage*image = [[UIImagealloc]initWithCGImage:imageRefRect];
//UIGraphicsBeginImageContext(mainView.bounds.size);
//[mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
//UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
//
//UIGraphicsEndImageContext();
//显示截图
//UIImageView *imaView = [[UIImageView alloc] initWithImage:image];
//imaView.frame = CGRectMake(0, 0, mainView.frame.size.width/2, mainView.frame.size.height/2);
//[[UIApplication sharedApplication].keyWindow addSubview:imaView];
//写入相册
//UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
returnimage;
}