2

Is there a way to determine whether an existing SKTexture uses a @2x or @3x image version?

I could look at the texture's size and compare them but I was wondering if there's a more elegant way to do it, preferably without using a UIImage.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • PS: related question without answer here: http://stackoverflow.com/questions/29886540/how-to-get-the-original-image-scale-from-an-sktexture – CodeSmile Jul 14 '15 at 14:06
  • 1
    Are you looking to load images from your own atlas or Xcode's image assets? – sangony Jul 14 '15 at 14:16
  • There is a `CGImage` property on `SKTexture` on iOS 9 and OS X 10.11. You could probably determine the scale based on screen density and bytes per row or something like that. – Ben Kane Jul 14 '15 at 14:45
  • Just individual PNG images. No atlas, no asset catalog. – CodeSmile Jul 14 '15 at 15:00
  • "I could look at the texture's size and compare them but I was wondering if there's a more elegant way to do it" Texture size is still in points isn't it? – Skyler Lauren Jul 17 '15 at 16:37
  • Yup, that's in points. It's also unreliable in that it'll be incorrect if the PNG's dpi isn't set to 72 ppi (Inkscape defaults to 90 ppi with no way of changing it). See: http://stackoverflow.com/questions/19744111/sprite-kit-os-x-sktexture-size-property-is-incorrect – CodeSmile Jul 19 '15 at 08:45

1 Answers1

2

Here's a solution that should work for iOS 9 and OS X 10.11:

CGImageRef imageRef = texture.CGImage;
CGFloat *scale = CGImageGetWidth(imageRef) / [texture size].width;
CGImageRelease(imageRef); // Not sure if you need this line?
Ben Kane
  • 9,331
  • 6
  • 36
  • 58
  • Well, it's better than nothing I guess. Whether this will actually create a CGImage or whether SKTexture uses a CGImage internally already is a good question though. – CodeSmile Jul 14 '15 at 15:02
  • Yea you'll have to check the reference count I guess. I don't have OS X 10.11/Xcode 7 to test this – Ben Kane Jul 14 '15 at 15:02
  • @LearnCocos2D I'm curious, did you end up finding a good solution for this? – Ben Kane Nov 04 '15 at 04:13