我正在使用以下代码进行屏幕截图
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;
它工作正常,但它返回全屏,我想要一个特定帧的屏幕截图,如( 100,100,200,200)
,我试图进行更改:
UIGraphicsBeginImageContext(self.view.frame.size);
但没有成功。
答案 0 :(得分:9)
试试这个:
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();
答案 1 :(得分:0)
只需使用三行代码即可。
UIGraphicsBeginImageContext(CGSizeMake(320, 480));
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *frame= UIGraphicsGetImageFromCurrentImageContext();