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

查看:223
本文介绍了将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天全站免登陆