我有一个添加了 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/