IOS 截图

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    //UIView
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
    view.backgroundColor=[UIColor redColor];
    [self.view addSubview:view];
   
    //在UIImageView中显示截取的图片
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 200, 100)];
    imageView.image=[self screenShotView:view];
    [self.view addSubview:imageView];
}

// 对指定视图进行截图
- (UIImage *)screenShotView:(UIView *)view
{
    UIImage *imageRet = nil;
   
    if (view)
    {
        if(UIGraphicsBeginImageContextWithOptions != NULL)
        {
            UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
        }
        else
        {
            UIGraphicsBeginImageContext(view.frame.size);
        }
       
        //获取图像
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        imageRet = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }else{
    }
   
    return imageRet;
}

原文地址:https://www.cnblogs.com/zhibin/p/4137412.html