gpt4 book ai didi

ios - SpriteKit - 可以使用 SDWebImage 将 Facebook friend 的图像延迟加载到 spriteNodeWithTexture 中吗?

转载 作者:行者123 更新时间:2023-11-29 10:23:34 24 4
gpt4 key购买 nike

我正在构建一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com