截图

  // 1.开启图形上下文
    CGSize imageSize = self.view.bounds.size;
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    
    // 2.将某个view的所有内容渲染到图形上下文中
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    
    // 3.取得图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //截取320*240的图片 截取的事像素 所以要*2
    CGImageRef subimageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 640, 480));
    UIImage *subImage = [UIImage imageWithCGImage:subimageRef];
    [UIImagePNGRepresentation(subImage) writeToFile:@"/Users/apple/Desktop/view.png" atomically:YES];
    
    // 4.关闭上下文
    UIGraphicsEndImageContext();
原文地址:https://www.cnblogs.com/10-19-92/p/4965138.html