iOS Screenshot part of the screen

后端 未结 4 2118
野的像风
野的像风 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 09:58

    From this

    UIGraphicsBeginImageContext(sshot.frame.size);
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(c, 150, 150);    // <-- shift everything up to required position when drawing.
    [self.view.layer renderInContext:c];
    UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    
    0 讨论(0)
  • 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 :)

    0 讨论(0)
  • 2020-11-29 10:14

    Use this method to crop if u have image with specfic rect to crop:

    -(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
    {
       CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
       UIImage *img = [UIImage imageWithCGImage:imageRef]; 
       CGImageRelease(imageRef);
       return img;
    }
    

    Use like this:

    UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)]; //example
    
    0 讨论(0)
  • 2020-11-29 10:18

    Well the screenshot is taken from a canvas you draw. So instead of drawing your layer in the whole context, with a reference to top left corner, you will draw it where you want to take the screenshot....

    //first we will make an UIImage from your view
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //now we will position the image, X/Y away from top left corner to get the portion we want
    UIGraphicsBeginImageContext(sshot.frame.size);
    [sourceImage drawAtPoint:CGPointMake(-50, -100)];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
    
    0 讨论(0)
提交回复
热议问题