iOS Screenshot part of the screen

后端 未结 4 2121
野的像风
野的像风 2020-11-29 09:19

I have an App that takes a screenshot of a UIImageView with the following code:

-(IBAction) screenShot: (id) sender{

 UIGraphicsBeginImageContext(sshot.fram         


        
4条回答
  •  庸人自扰
    2020-11-29 10:12

    If you like you can refer this code.

    In this example you can get the image covered by the rect from any position and any zoom scale.

    Happy Coding :)

    Some extracted code for reference is as below

    Main function or code used to crop the photo

    - (UIImage *) croppedPhoto
    {
        CGFloat ox = self.scrollView.contentOffset.x;
        CGFloat oy = self.scrollView.contentOffset.y;
        CGFloat zoomScale = self.scrollView.zoomScale;
        CGFloat cx = (ox + self.cropRectangleButton.frame.origin.x + 15.0f) * 2.0f / zoomScale;
        CGFloat cy = (oy + self.cropRectangleButton.frame.origin.y + 15.0f) * 2.0f / zoomScale;
        CGFloat cw = 300.0f / zoomScale;
        CGFloat ch = 300.0f / zoomScale;
        CGRect cropRect = CGRectMake(cx, cy, cw, ch);
    
        NSLog(@"---------- cropRect: %@", NSStringFromCGRect(cropRect));
        NSLog(@"--- self.photo.size: %@", NSStringFromCGSize(self.photo.size));
    
        CGImageRef imageRef = CGImageCreateWithImageInRect([self.photo CGImage], cropRect);
        UIImage *result = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
    
        NSLog(@"------- result.size: %@", NSStringFromCGSize(result.size));
    
        return result;
    }
    

    The details how to use the example is given here.

    Enjoy Coding :)

提交回复
热议问题