Capturing of Screen shot starting from 0,0

2019-02-21 02:55发布

问题:

I am using following code to make a screen shot

UIGraphicsBeginImageContext(self.view.frame.size);

blendMode:kCGBlendModeClear alpha:1.0];

[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

return viewImage;

It is working fine but it returns the full screen, and I want a screen shot of a particular Frame, like ( 100,100,200,200), I tried to make changes in:

UIGraphicsBeginImageContext(self.view.frame.size);

But no success.

回答1:

Try this:

float x = 100;
float y = 100;
CGSize size = CGSizeMake(200,200);

UIGraphicsBeginImageContext(size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, -x, -y);
[view.layer renderInContext:c];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


回答2:

just use three line of code.

    UIGraphicsBeginImageContext(CGSizeMake(320, 480));
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *frame= UIGraphicsGetImageFromCurrentImageContext();