I've now filed a bug for the issue below. Anyone with a good workaround?
我尝试将 SKTexture 保存到文件中,然后再次加载它,但我没有成功。可以将以下代码片段复制到 Xcode 启动项目中的 GameScene.m。
我在 generateTexture 中使用了 textureFromNode,这似乎是我的问题的根本原因。如果我使用来自 sprite 的纹理,代码可以运行,并且可以看到两艘宇宙飞船。
此代码在 iOS 8 中有效,但在 Xcode7 和 iOS 9 中停止工作。我只想在提交错误报告之前验证这是一个错误。我担心的是我对 NSKeyedArchiver 做错了什么。
它在模拟器和设备上都会发生。
#import "GameScene.h"
@implementation GameScene
// Generates a texture
- (SKTexture *)generateTexture
{
SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];
SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(50, 50)];
shapeNode.position = CGPointMake(50, 50);
shapeNode.strokeColor = SKColor.redColor;
shapeNode.lineWidth = 10;
[scene addChild:shapeNode];
SKTexture *texture = [self.view textureFromNode:scene];
//SKTexture *texture = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"].texture; // This works!
return texture;
}
// Just generate a path
- (NSString *)fullDocumentsPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourFileName = [documentsDirectory stringByAppendingPathComponent:@"fileName"];
return yourFileName;
}
- (void)didMoveToView:(SKView *)view
{
self.scaleMode = SKSceneScaleModeResizeFill;
// Verify that the generateTexture method indeed produces a valid texture.
SKSpriteNode *s1 = [SKSpriteNode spriteNodeWithTexture:[self generateTexture]];
s1.position = CGPointMake(100, 100);
[self addChild:s1];
// Start with saving the texture.
NSString *fullName = [self fullDocumentsPath];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
if ([fileMgr fileExistsAtPath:fullName])
{
[fileMgr removeItemAtPath:fullName error:&error];
assert(error == nil);
}
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:[self generateTexture] forKey:@"object"];
bool ok = [NSKeyedArchiver archiveRootObject:dict1 toFile:fullName];
assert(ok);
// Read back the texture and place it in a sprite. This sprite is not shown. Why?
NSData *data = [NSData dataWithContentsOfFile:fullName];
NSDictionary *dict2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
SKTexture *loadedTexture = [dict2 objectForKey:@"object"];
SKSpriteNode *s2= [SKSpriteNode spriteNodeWithTexture:loadedTexture];
NSLog(@"t(%f, %f)", loadedTexture.size.width, loadedTexture.size.height); // Size of sprite & texture is zero. Why?
s2.position = CGPointMake(200, 100);
[self addChild:s2];
}
@end
Update for Yudong:
这可能是一个更相关的示例,但假设场景由 4 个层组成,有很多 Sprite 。比赛结束后,我想存储比赛结束场景的缩略图。该图像将用作按钮上的纹理。按下该按钮将开始重播比赛的电影。会有很多带有旧游戏图像的按钮,所以我需要将每个图像存储在文件中。
-(SKTexture*)generateTexture
{
SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];
SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
ship.position = CGPointMake(50, 50);
[scene addChild:ship];
SKTexture *texture = [self.view textureFromNode:scene];
NSLog(@"texture: %@", texture);
return texture;
}
The solution/work around:
受 Russells 代码的启发,我做了以下事情。有用!
CGImageRef cgImg = texture.CGImage;
SKTexture *newText = [SKTexture textureWithCGImage:cgImg];
最佳答案
我已经对 SKTextures 进行了大量试验/修改。我的游戏使用 SKTextures。它是用 Swift 编写的。具体来说,我在 textureFromNode 和 textureFromNode:crop: 以及从纹理创建 SKPhysicsBodies 方面遇到了很多问题。这些方法在 ios 8 中运行良好,但 Apple 在发布 ios 9.0 时完全破坏了它们。在 ios 9.0 中,纹理返回为 nil。那些零纹理从纹理中破坏了 SKPhysicsBodies。
我最近从事 SKTextures 的序列化/反序列化工作。
您可能会调查的一些关键想法/线索是:
运行 ios 9.2。 Apple Staff 提到很多问题已得到修复。 https://forums.developer.apple.com/thread/17463我发现 ios 9.2 对 SKTextures 有帮助,但并没有解决所有问题,尤其是序列化问题。
尝试 PrefersOpenGL(在您的配置中将其设置为“YES”作为 bool 自定义属性)。这是 Apple 员工在 Apple Dev Forums 中关于 PrefersOpenGL 的帖子。 https://forums.developer.apple.com/thread/19683我观察到 ios 9.x 似乎默认使用 Metal 而不是 OpenGL。我发现 PrefersOpenGL 有助于解决 SKTexture 问题,但仍然无法使我的 SKShaders 工作(用 GLSL 编写)。
当我尝试在 ios 9.2 上使用 SKTextures 序列化/反序列化节点时,我得到的是白色框而不是可见纹理。受 Apple SKTexture 文档的启发,“纹理数据在以下情况下加载:
调用纹理对象的大小方法。
调用另一个需要纹理大小的方法,例如创建一个使用纹理对象的新 SKSpriteNode 对象。
其中一种预加载方法被调用(请参阅预加载纹理数据。)
纹理数据在以下情况准备渲染:
使用纹理的 Sprite 或粒子是正在渲染的节点树的一部分。”
...我破解了一个从 CGImage() 调用创建辅助纹理的变通方法:
// ios 9.2 workaround for white boxes on serialization
let img = texture!.CGImage()
let uimg = UIImage(CGImage: img)
let ntex = SKTexture(image: uimg)
let sprite = SKSpriteNode(texture: ntex, size: texture!.size())
所以现在我以这种方式创建的 SKSpriteNode 似乎可以很好地序列化/反序列化。顺便说一句,仅调用 size() 或使用原始纹理创建 SKSpriteNode 似乎不足以将纹理具体化到内存中。
希望对您有所帮助。我想我对 SKTextures 的挣扎比大多数人都多,因为我的应用程序非常依赖它们。
关于ios - 将生成的 SKTexture 保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32701576/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A