将带有添加子视图的 UIView 制作成单个图像? [英] make a UIView with added subviews into a single image?

查看:17
本文介绍了将带有添加子视图的 UIView 制作成单个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个添加了子视图的视图.我想将具有许多子视图的视图转换为单个图像或视图.

I have a view with added subviews. I would like to turn that view with many subviews into a single image or view.

这怎么可能?
谢谢

How is that possible?
Thanks

推荐答案

在 iOS7 上你可以使用新的 [UIView snapshotViewAfterScreenUpdates:] 方法.

On iOS7 you can use the new [UIView snapshotViewAfterScreenUpdates:] method.

为了支持较旧的操作系统,您可以使用 Core Graphics 将任何视图渲染到 UIImage 中.我在 UIView 上使用此类别进行快照:

To support older OS you can render any view into an UIImage with Core Graphics. I use this category on UIView for snapshots:

UView+Snapshot.h:

#import <UIKit/UIKit.h>

@interface UIView (Snapshot)
- (UIImage *)snapshotImage;
@end

UView+Snapshot.m:

#import "UIView+Snapshot.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIView (Snapshot)

- (UIImage *)snapshotImage
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

@end

它需要 QuartzCore 框架,因此请确保将其添加到您的项目中.

It requires QuartzCore framework, so make sure to add that to your project.

要使用它导入标题和:

UIImage *snapshot = [interestingView snapshotImage];

这篇关于将带有添加子视图的 UIView 制作成单个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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