只是在玩 SpriteKit,并试图弄清楚如何将 SKNode 的“抓取”捕获到 UIImage 中。
对于 UIView (或 UIView 子类),我使用了layer
要渲染到图形上下文中的视图的属性。
Eg.
#import <QuartzCore/QuartzCore.h>
+ (UIImage *)imageOfView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *viewShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewShot;
}
SKNode 不是 UIView 的子类,因此似乎不受层支持。
关于如何将给定的 SKNode 渲染到 UIImage 的任何想法?
这将捕获整个场景:
CGRect bounds = self.scene.view.bounds;
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
UIImage* screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
如果您只想要一个特定的节点分支,您可以在截图之前隐藏您不想捕获的所有节点。您还可以使用累积帧转换为UIKit
坐标以仅捕获有问题的节点及其子节点的区域。
或者,您可以获得SKTexture
从节点层次结构的特定部分:
SKTexture* tex = [self.scene.view textureFromNode:yourNode];
在 iOS 9 之前,没有办法转换SKTexture
回到一个UIImage
。然而,现在这很微不足道:
UIImage *image = [UIImage imageWithCGImage:tex.CGImage];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)