【问题标题】:SpriteKit - Possible to lazy load a Facebook friend's image into a spriteNodeWithTexture using SDWebImage?SpriteKit - 可以使用 SDWebImage 将 Facebook 朋友的图像延迟加载到 spriteNodeWithTexture 中吗?
【发布时间】:2015-11-06 09:19:15
【问题描述】:

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

所以我正在探索将我朋友的个人资料图片延迟加载到 SKSpriteNodes 中的方法,以减少加载关卡选择屏幕时的等待时间,并在下载后自动显示图片。我目前正在尝试使用 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 完成延迟加载有任何见解,将不胜感激。

谢谢!

【问题讨论】:

    标签: ios facebook-graph-api sprite-kit


    【解决方案1】:

    在搜索了将图像“延迟加载”到 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"]]];
    

    希望这对将来的某人有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      相关资源
      最近更新 更多