Recently the company project finished! Idle every day only write Jane book! Sum up everything you’ve summed up before! 😂 😂 😂

This includes: capturing a screen, capturing a view, and capturing a range of the specified view!

The method is as follows:

CutScreen {return [self cutFromView:[UIApplication sharedApplication].keyWindow]; } /** get the image from a view */ -(UIImage *)cutFromView:(UIView *)view {// enable the graphics context UIGraphicsBeginImageContextWithOptions (view. Frame. The size, NO, 0.0 f); / / get the context CGContextRef context = UIGraphicsGetCurrentContext (); if (! context) { return nil; } // Render the view's layer [view.layer renderInContext:context] in the context of the new graph; // Set the color: transparent [[UIColor clearColor] setFill]; / / get photo UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); / / close the graphics context UIGraphicsEndImageContext (); return image; } /** get the image in the specified range from a view */ -(UIImage *)cutImageFromView:(UIView *)view andFrame:(CGRect)rect {// enable the graphics context UIGraphicsBeginImageContext(view.frame.size); / / get the context CGContextRef context = UIGraphicsGetCurrentContext (); if (! context) { return nil; } CGContextSaveGState(context); UIRectClip(rect); // Render the view's layer [view.layer renderInContext:context] in the new graphics context; // Set the color: transparent [[UIColor clearColor] setFill]; / / get photo UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); / / close the graphics context UIGraphicsEndImageContext (); return image; }Copy the code

### show examples 🌰 in ViewController:

  • Setting global variables

    @interface ViewController () { UIButton * _button; UIImageView * _showImg_View; // Display the image} @endCopy the code
  • In “- (void)viewDidLoad {}”, set the interface layout

    _button = [[UIButton alloc] init]; [_button setBackgroundImage:[UIImage imageNamed:@"im"] forState:UIControlStateNormal]; _button.backgroundColor = [UIColor cyanColor]; _button.frame = CGRectMake(0, 0, 300.f, 300.f); _button.center = CGPointMake(self.view.center.x, (300.f+0.f)/2); [_button addTarget:self action:@selector(clickToShowScreenCut) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_button]; _showImg_View = [[UIImageView alloc] init]; _showImg_View.image = [UIImage imageNamed:@"im"]; // Set the background color for easy observation! _showImg_View.backgroundColor = [UIColor greenColor]; _showImg_View.frame = CGRectMake(0, 0, 200.f, 200.f); _showImg_View.center = CGPointMake(self.view.center.x, _button.center.y+300.f); _showImg_View.layer.borderColor = [UIColor lightGrayColor].CGColor; _showImg_View.layer.borderWidth = 1.f; [self.view addSubview:_showImg_View];Copy the code

    Effect:


-(void)clickToShowScreenCut {}

  • screenshots

    _showimg_view. image = [self cutScreen];Copy the code

    Effect: capture the corresponding picture on the screen and put it in the picture view shown below!!

  • Capture view (button: _button)

    Image = [self cutFromView:_button];Copy the code

    Effect: Capture the image corresponding to the button

  • Captures the image within a (random) range of the view (button: _button)

    float random_X = arc4random()%100;
    float random_Y = arc4random()%100;
    NSLog(@"random_X:%lf  random_Y:%lf",random_X,random_Y);
    _showImg_View.image = [self cutImageFromView:_button andFrame:CGRectMake(random_X, random_Y, _button.frame.size.width-random_X, _button.frame.size.height-random_Y)];
    Copy the code

    Effect: Capture the image corresponding to the button in a random range

    ###### problem: the scope of the captured image is the overall scope of the captured view! Creating a “partially transparent” situation!!

    Solution: use “CGImageCreateWithImageInRect” get within a certain range cgImage!! Get the right picture!!

    */ -(UIImage *)cutImageFromView:(UIView *)view andFrame:(CGRect)rect {// enable the graphics context UIGraphicsBeginImageContext(view.frame.size); / / get the context CGContextRef context = UIGraphicsGetCurrentContext (); CGContextSaveGState(context); UIRectClip(rect); // Render the view's layer [view.layer renderInContext:context] in the new graphics context; // Set the color: transparent [[UIColor clearColor] setFill]; / / get photo UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); / / to get a picture (the rect) within the specified range cgImage CGImageRef cgImage = CGImageCreateWithImageInRect (image. CgImage, the rect); UIImage * returnImage = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); / / close the graphics context UIGraphicsEndImageContext (); return returnImage; }Copy the code

    Effect: the range of images obtained is correct!

Image saved locally:

NSData *data = UIImageJPEGRepresentation([self cutScreen], 1); / / to save the corresponding data file for the picture [data writeToFile: [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents/blabla. JPG"] atomically:YES];Copy the code

(2017.11.27)

goyohol’s essay