如何捕获self.view的特定大小 [英] How to capture a specific size of the self.view

查看:79
本文介绍了如何捕获self.view的特定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有self.view,但是我只想捕获其中的300x320。

I have the self.view, but I only want to capture 300x320 of it.

获得此代码:

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;

我需要在此处进行哪些更改?

What do I need to change here in order to do so?

感谢分配!

推荐答案

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

Just change the size that you want to capture, instead of using the entire bounds, use the size you want. The render will start at the origin of the view and be clipped when it falls outside the bounds. If you need to change where the view is actually clipped, simply translate the context after starting the image context:

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

例如,如果您的视图是600x320,并且您想捕获中间300个宽度的点,您可以将上下文向左平移150点:

If your view was, for example, 600x320 and you wanted to capture the middle 300 points in width, you'd translate the context 150 points to the left:

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

这篇关于如何捕获self.view的特定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆