ios how to capture a particular portion of screen

后端 未结 6 1903
一生所求
一生所求 2020-12-14 11:41

I want to capture a particular portion of iPhone screen. I used UIGraphicsBeginImageContextWithOptions, but couldn\'t capture a portion of screen with this. Ple

6条回答
  •  醉话见心
    2020-12-14 12:39

    the answer by @Superdev is right on spot , what i want to add is a little trick if you want to capture a parent view and avoid subviews ( in particular overlay view ) what you can do is make this subview a property and use the following

    CGFloat width = CGRectGetWidth(self.view.bounds);
    CGFloat height = CGRectGetHeight(self.view.bounds);
    _overlayView.hidden = YES;
    UIGraphicsBeginImageContext(self.view.bounds.size);
    
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    CGRect rect = CGRectMake(0,0 ,width, height);
    CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
    
    UIImage *img = [UIImage imageWithCGImage:imageRef];
    
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
    
    CGImageRelease(imageRef);
    
    _overlayView.hidden = NO;
    

    its a small trick , hope someone will find it useful

提交回复
热议问题