如何捕捉self.view的特定大小

我有self.view,但我只想捕获它的300×320。

得到这个代码:

UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); UIImage * imgPrintScreen = [[UIImage alloc]init]; imgPrintScreen = viewImage; 

为了做到这一点,我需要改变什么?

谢谢!

只需更改要捕获的大小,而不是使用整个边界,请使用所需的大小。 渲染将从视图的原点开始,并在超出边界时剪切。 如果您需要更改视图实际被剪裁的位置,只需在启动图像上下文后转换上下文:

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.); [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

例如,如果您的视图是600×320,并且您想要捕获中间的300点宽度,则需要将上下文150点转换为左侧:

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, -150.f, 0.f); [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
Interesting Posts