nicklockwood/JPNG

Possible to swizzle imageNamed: ?

ishuo opened this issue · 8 comments

I love this idea and this project!! We were having many large images that need transparency, it went even worse after we made the project retina-ready. In numbers, this reduced our bundle from 27MB to 10MB 👍

I noticed that +[NSImage imageNamed:] (and +[UIImage imageNamed:]) is not swizzled yet. Are there special considerations / concerns for the lack of it? Will it be supported? :)

This class method handles the DPI / device suffix (@2x) and the size of the image files automatically, which is more convenient than -[NS/UIImage imageWithData:].

imageNamed: is tricky due to the private caching mechanism, but I'll see what I can do.

OK, I have a solution. Will release shortly.

Wow that was fast :) Thanks in advance!

Fixed in version 1.1.

I guess there is a problem :) I always give imageNamed: a filename without the extension (testImage), it then does the extra thing: it appends the appropriate suffix (like @2x, maybe also ~ipad) and chooses the correct concrete file (testImage@2x.png on retina, testImage.png on normal displays). It also set the width and height of the image appropriately. You noticed that too?

This is missing in the current JPNG lib. However, it seems like that this functionality is quite complex and have to be implemented (and maintained) manually :(

That should already work (adding the @2x, ~ipad, etc) but you will need to include the .jpng extension.

So if you try to load "TestImage.jpng" it will automatically load "TestImage@2x.jpng" on a Retina device.

I'll consider adding automatic support for no file extension as well, but note that Apple only support it for PNG normally, not jpeg or other formats.

And yes, the logic is quite complex, but I do it anyway :-)

If you like that, you might want to check out my StandardPaths library, which implements the @2x and ~ipad functionality for all file types and also adds support for the -568h@2x extension so that you can automatically load different images for iPhone 5 just by setting file name.

You are right, it already works with the @2x suffix! Sorry!

StandardPaths is interesting! :)