Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...