Swift: Cropping a Screenshot without TabBar and NavigationBar

前端 未结 5 850
臣服心动
臣服心动 2021-01-05 14:43

I have a screenshot of the entire screen, screenshot, generated using the following:

let layer = UIApplication.sharedApplication().keyWindow!.l         


        
5条回答
  •  南方客
    南方客 (楼主)
    2021-01-05 15:14

    swift:

    let contextImage: UIImage = <>!
    let cropRect: CGRect = CGRectMake(x, y, width, height)
    let imageRef: CGImageRef = CGImageCreateWithImageInRect(contextImage.CGImage, cropRect)
    let image: UIImage = UIImage(CGImage: imageRef, scale: originalImage.scale, orientation: originalImage.imageOrientation)!
    

    obj-c:

    UIImage *image = <>;
    
    CGRect croprect = CGRectMake(0, 0,
    self.view.bounds.width,
    self.view.bounds.height + self.navigationController!.navigationBar.frame.height));
    
    // Draw new image in current graphics context
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], croprect);
    
    // Create new cropped UIImage
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    

提交回复
热议问题