ios - 将添加了 subview 的 UIView 制作成单个图像?

标签 ios uiview

我有一个添加了 subview 的 View 。我想将具有许多 subview 的 View 转换为单个图像或 View 。

这怎么可能?
谢谢

最佳答案

在 iOS7 上,您可以使用新的 [UIView snapshotViewAfterScreenUpdates:] 方法。

为了支持旧操作系统,您可以使用 Core Graphics 将任何 View 渲染到 UIImage 中。我在 UIView 上使用这个类别进行快照:

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 框架,因此请务必将其添加到您的项目中。

要使用它,请导入 header 和:

UIImage *snapshot = [interestingView snapshotImage];

关于ios - 将添加了 subview 的 UIView 制作成单个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11146525/

相关文章:

objective-c - 在 iOS 上加载 UIView 时如何运行一些代码?

ios - 如何设置 View 大小以匹配 iOS 中的设备屏幕?

ios - 你能在 NSObject 类中将 Target 添加到 UIButton 吗?

objective-c - 如何将底部阴影添加到具有动态高度的表格单元格?

objective-c - 动画 UIView 原点仅在第一次尝试后才有效

iphone - 在模拟器中第二次运行 ios 应用程序时登录

ios - 在 super.init 初始化 self 之前在方法调用中使用 self

ios - 在适用于 iOS 的 Adob​​e native 扩展中显示和定位 UISwitch 或任何 UIView

ios - 使用点击从视频中获取缩略图

ios7 iPad横向模式下的错误 View 大小