将 UIView 捕获为 UIImage [英] Capture UIView as UIImage

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

问题描述

我一直在使用这种方法将 UIView 转换为 UIImage.即视图的屏幕快照 -

I have been using this method to convert a UIView into UIImage. i.e. screen snapshot of a view -

@interface UIView(Extended) 

- (UIImage *) imageByRenderingView;

@end


@implementation UIView(Extended)


- (UIImage *)imageByRenderingView
{   
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

@end

为了使用它,我这样做 -

To use it, I do this -

UIImage *currImage = [self.view imageByRenderingView];

这给出了整个 UIView 的图像表示.现在我想要 2 张图片,一张是 UIView 的上半部分,另一个是下半部分.我该怎么做?

This gives the image representation of the entire UIView. Now I want 2 images, one is of the top half of the UIView and the other is the bottom half. How do I do that?

推荐答案

您可以使用以下代码将 UIImage 一分为二:

You can split your UIImage in two by using this code:

CGImageRef topOfImageCG =
         CGImageCreateWithImageInRect(currImage.CGImage,
                                      CGRectMake(0,
                                                 0,
                                                 currImage.size.width,
                                                 currImage.size.height / 2.0));

UIImage *topOfImage = [UIImage imageWithCGImage:topOfImageCG];

CGImageRelease(topOfImageCG);

这篇关于将 UIView 捕获为 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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