How can I add JPEGs to an asset catalog in Xcode?

XcodeAssetsXcode5

Xcode Problem Overview


Is it possible to add assets other than PNG files to an Xcode Asset Catalog?

When I drag JPEG files into an Asset Catalog they aren't accepted by the UI.

Xcode Solutions


Solution 1 - Xcode

You can add non-PNG assets by editing the JSON representation of the asset manually. The easiest way is to copy an existing asset and modify it:

  1. Right click on an existing asset and choose Show in Finder
  2. Copy and paste the existing .imageset item and rename it, e.g. my_image.imageset
  3. Double-click the new .imageset
  4. Delete any existing images in the folder
  5. Copy in your JPEG files
  6. Edit the Contents.json file, replacing the values for the filename key with your JPEG filenames

Your Contents.json will look something like this:

{
  "images" : [
    {
      "idiom" : "universal",
      "scale" : "1x",
      "filename" : "my_image.jpg"
    },
    {
      "idiom" : "universal",
      "scale" : "2x",
      "filename" : "[email protected]"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

Be sure to refer to your image by name, without extension:

[UIImage imageNamed:@"my_image"]

This approach will work for GIFs and other assets as they are just copied into the App's main bundle at build time. It is worth noting that the images end up with a png extension when copied to the bundle, but they still load correctly.

Solution 2 - Xcode

As of Xcode 6.1 JPG images can now be added to an asset catalog. The steps to add are:

  1. Export your JPG at the desired quality using the same naming conventions as for PNGs (i.e. heart.jpg [email protected] [email protected])
  2. Drag the JPGs into your asset catalogue from Finder and add any splicing or device specific settings to the asset
  3. Change the 'Render As' to be 'Original Image' for all JPGs (otherwise they'll appear blank in the simulator and on the device)

Solution 3 - Xcode

It appears you can now drag and drop JPEGs in Xcode 6.0 Beta

Solution 4 - Xcode

In Xcode 6.x you can drag and drop jpegs to the asset catalog. If creating a UIImage from the asset make sure you use the .jpg extension like so:

[UIImage imageNamed:@"myimage.jpg"];

If the extension is not included, the image will just appear white/blank.

Solution 5 - Xcode

You can also rename your image.jpg to image.png if you don't want to change the Contents.json file. You can then add the images to the asset catalog, even though internally they are still jpeg files. You can even slice them using Xcode.

When the application is compiled, all assets go into the Assets.car file. I haven't verified if at this point they get converted to png.

Solution 6 - Xcode

Try this. Quickly import resource scripts to resolve import speed and rename issues. https://github.com/qdvictory/happyxcasset

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid Snabel-CauntView Question on Stackoverflow
Solution 1 - XcodeDavid Snabel-CauntView Answer on Stackoverflow
Solution 2 - XcodeKevin SylvestreView Answer on Stackoverflow
Solution 3 - XcodeDanView Answer on Stackoverflow
Solution 4 - XcodegavdotnetView Answer on Stackoverflow
Solution 5 - XcodeCarlos ParadaView Answer on Stackoverflow
Solution 6 - XcodeSeamusView Answer on Stackoverflow