SpriteKit-是否可以使用SDWebImage将Facebook朋友的图像延迟加载到spriteNodeWithTexture中?

约翰·R

我正在使用Facebook图构建SpriteKit游戏,以检索要显示在“关卡选择”屏幕上的朋友(也使用该应用程序)的图像。级别选择屏幕之前加载得相当快,但是我注意到我需要加载的好友图像越多,显示级别选择屏幕所花费的时间就越长。(直到所有图像加载完毕,屏幕才会显示。)

因此,我正在探索将朋友的个人资料照片延迟加载到SKSpriteNodes中的方法,以减少加载“级别选择”屏幕时的等待时间,并在下载图像后自动显示它们。我目前正在尝试使用SDWebImage,但尚未看到有关如何将SpWebKit与SpriteKit一起使用的任何示例。

到目前为止,这就是我要做的。SKSpriteNode可以毫无问题地加载占位符图像,但是看起来Sprite Node并不关心在应用占位符图像之后刷新其图像。

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完成延迟加载有任何见解,将不胜感激。

谢谢!

约翰·R

在寻找其他将图像“延迟加载”到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"]]];

希望这对以后的人有所帮助。:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章