- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 SpriteKit 游戏,它利用 Facebook 图来检索要在关卡选择屏幕上显示的 friend (也使用该应用程序)的图像。关卡选择屏幕之前加载得相当快,但我注意到我需要加载的 friend 图像越多,关卡选择屏幕显示所需的时间就越长。 (直到所有图片都加载完毕,屏幕才会出现。)
因此,我正在探索将我 friend 的个人资料图片延迟加载到 SKSpriteNode 中的方法,以减少加载关卡选择屏幕时的等待时间,并让图片在下载后自动显示。我目前正在尝试使用 SDWebImage 但还没有看到任何关于如何将 SDWebImage 与 SpriteKit 一起使用的示例。
这是我目前所做的。 SKSpriteNode 将毫无问题地加载占位符图像,但似乎 Sprite 节点并不关心在应用占位符图像后刷新它的图像。
UIImageView *friendImage = [[UIImageView alloc] init];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?redirect=true", {FRIEND ID GOES HERE}]];
[friendImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"user_placeholder_image"]];
SKSpriteNode* playerPhoto = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImage:friendImage.image]];
[self addChild: playerPhoto];
如果有人对如何使用 SpriteKit 完成延迟加载有任何见解,将不胜感激。
谢谢!
最佳答案
在寻找将图像“延迟加载”到 SKSpriteNode 的其他方法后,我发现了 Grand Central Dispatch (GCD) 是什么以及它如何为我尝试做的事情工作。我实际上不需要使用 SDWebImage。
这是我用来获得完美结果的代码:
// init the player photo sprite node - make sure to use "__block" so you can use the playerPhoto object inside the block that follows
__block SKSpriteNode* playerPhoto = [[SKSpriteNode alloc] init];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?redirect=true", {FRIEND FB ID GOES HERE}]]];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
// update player photo texture after image was downloaded
playerPhoto.texture = [SKTexture textureWithImage:image];
NSLog(@"fb image downloaded and applied!");
});
});
// use a placeholder image while fb image downloads
playerPhoto = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImage:[UIImage imageNamed:@"user_placeholder_image"]]];
希望这对以后的人有所帮助。 :)
关于ios - SpriteKit - 可以使用 SDWebImage 将 Facebook friend 的图像延迟加载到 spriteNodeWithTexture 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563285/
我正在构建一个 SpriteKit 游戏,它利用 Facebook 图来检索要在关卡选择屏幕上显示的 friend (也使用该应用程序)的图像。关卡选择屏幕之前加载得相当快,但我注意到我需要加载的 f
我是一名优秀的程序员,十分优秀!