How to Use Uiimage(Contentsoffile:String) Method to Load Images from Images.Xcassets Folder

UIImage using contentsOfFile

If you need to use pathForResource() for avoiding image caching, it is not possible to work with images.xcassets. In that case you need to create group in XCode, and add image there (be sure, that image is copied to Copy Bundle Resources). Afterwards just write:

Swift 5:

let bundlePath = Bundle.main.path(forResource: "imageName", ofType: "jpg")
let image = UIImage(contentsOfFile: bundlePath!)

Older Swift:

let bundlePath = NSBundle.mainBundle().pathForResource("imageName", ofType: "jpg") 
let image = UIImage(contentsOfFile: bundlePath!)

How can I use UIImage(contentsOfFile:String) with image data downloaded from the internet?

If you don't have much time, you should just use SDWebImage. It does all this for you, the right way.

How to load an image in prepareForInterfaceBuilder with a IBDesignable UIImageView

Updated for Swift 4.2

When you instantiate an UIImage (with UIImage(named : "SomeName") the app will look for the asset in your main bundle, which works fine usually. But when you are at design time, the InterfaceBuilder holds the code of the designable views (for compiling while designing) in a separate bundle.

So the solution is: Define your bundle dynamically, hence your files can be found in design, compile and run time:

    // DYNAMIC BUNDLE DEFINITION FOR DESIGNABLE CLASS
let dynamicBundle = Bundle(for: type(of: self))

// OR ALTERNATIVELY BY PROVDING THE CONCRETE NAME OF YOUR DESIGNABLE VIEW CLASS
let dynamicBundle = Bundle(for: YourDesignableView.self)

// AND THEN SUCCESSFULLY YOU CAN LOAD THE RESSOURCE
let image = UIImage(named: "Logo", in: dynamicBundle, compatibleWith: nil)

Access Asset Catalog pathForResource

Same problem, but I don't think we could access it via pathForResource:, except below:

UIImage* image = [UIImage imageNamed:@"name-in-asset-catalog"];

It has been clearly documented:

Each set in an asset catalog has a name. You can use that name to
programmatically load any individual image contained in the set. To
load an image, call the UIImage:imageNamed: method, passing the name
of the set that contains the image.

I found that in the package of complied app, a file named "Assets.car" came out, and I think it's the entire images sets in my project, and should have been zipped or something.

Refer to Apple's documentation:

Xcode 5 provides different functionality for asset catalogs depending
on the deployment target for your project:

  • For all projects, individual images can be loaded using set names.
  • For projects with a deployment target of iOS 7, Xcode compiles your asset
    catalogs into a runtime binary file format that reduces the download
    time for your app.

So that's it.

I'm also looking for a way that doesn't use imageNamed:, I don't want runtime to cache my images.

Swift playgrounds with UIImage

  1. Open the .playground file in Finder.
  2. Create a folder called Resources next to it.
  3. Add any images you want to this folder.
  4. In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.

File Inspector


  1. Under 'Resource Path' choose 'Relative To Playground'
  2. Click the folder icon underneath and choose the Resources folder created earlier.

You should now have a bundle that you can use with the standard NSImage(named:"filename_without_extension"):

Working nsbundle image

Note: Because Xcode will frequently overwrite the .playground folder, I recommend using this method so the resources folder isn't getting constantly deleted and re-created.

Load image from bundle with iOS

That is correct, imageNamed: will search your main bundle. Images in your project, even if they are in different groups in your Project Navigator will be in your main bundle and can be accessed directly by name.

Unable to load images from framework in my project

Before Building and distributing your framework, you should copy files that need to carry with your framework.

To copy files, Click your framework project target, go to "Build Phases" tab and you can see a section called "Copy Files". Add your files here(See below screenshot)

Sample Image

Then build your framework project, after a successful build your distributable framework will be generated in your Products folder. It will look like YourFrameworkProjectName.framework.

Add this file to other projects which need your framework.

Note: If you just want to run your project by connecting with your framework project, you can be done it by adding build dependency. See StackOverflow question here

Xcode - Adding an image to a test?

Your problem is, that you search for the image in the main bundle. So at the moment you access the mainBundle where your image doesn't exists, because it's in the test-bundle.

So you need to access another bundle. The bundle where your testclass is nested.

For that, use bundleForClass instead of mainBundle:

//The Bundle for your current class
var bundle = NSBundle(forClass: self.dynamicType)
var path = bundle.pathForResource("example_image", ofType: "jpg")

As you see, you load the NSBundle for your class and you should now be able to access the image. You could also add the image to your main target and use the mainBundle again.



Related Topics



Leave a reply



Submit