-1

So, I noticed this recommendation in one of the discussions here. It says that in order to release memory once you done with the textures, a possible solution will be the next:

  1. create UIImages with +imageWithContentsOfFile:
  2. create SKTextures from the resulting UIImage objects by calling SKTexture/+textureWithImage:(UIImage*).

I have memory issues that comes from the high res backgrounds and few embedded scroll views, so I tried the above implementation - and it works in terms of memory.

The problem is that it looks like when doing so, imageWithContentsOfFile: in retina devices, scales the @2x image by 2, so I need to explicit scale it again by 0.5.

How can I ensure the right scaling when calling this method?

Here is my code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"sc_01_bg" ofType:@"png"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
SKSpriteNode *backgroundSprite = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImage: img]];
Community
  • 1
  • 1
DocForNoc
  • 346
  • 2
  • 13

1 Answers1

0

Actually, there is only upvote on that answer, and that upvote is mine. I can't remember now why I have upvoted that answer, because I've never experienced such problem.

If you still want to use the approach stated in that answer, you should use [UIImage imageNamed:@"imageName"] instead of [UIImage imageWithContentsOfFile:path]. Probably, UIKit doesn't automatically handle retina/non-retina images with imageWithContentsOfFile: method.

But basically you don't need that. Check LearnCocos2D's answer for that question.

PS: To free up a memory that have been used by SKScene just call

[self.skView removeFromSuperview];
self.skView = nil;
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162