7
只是玩弄SpriteKit,並試圖弄清楚如何捕捉一個SKNode到'UIImage'的'抓取'。如何渲染一個SKNode到UIImage
使用UIView(或UIView子類),我已經使用視圖的layer
屬性渲染到圖形上下文中。
例如,
#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();
:
謝謝。結果將再次用於Sprite Kit - so textureFromNode:非常出色。乾杯! –
@ learncocos2d這是否仍然是最好的方法? –
@StevenRitchie不幸的是。 – ColdSteel