Capturing of Screen shot starting from 0,0

亡梦爱人 提交于 2019-12-19 11:18:03

问题


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();


来源:https://stackoverflow.com/questions/5538080/capturing-of-screen-shot-starting-from-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!