How to get array of UIImage, from folder, in Swift?

get image from document directory swift 4
how to save image in document directory in swift 4
image array in swift
how to create image array in swift 4
empty image array in swift
array of image swift 4
swift uiimage
get all images from document directory ios swift

I have an ordinary Xcode project like this ...

notice there's a folder (it is an actual folder - not just a group) named "images". It contains 25 ".png" images.

All I want to do is make an array of UIimage with each of those images.

(Or even, an array of the image names or similar, that would be fine - then could load them UIImage(named:)

How do I get at that folder "images"?? What about a subfolder "images/cars"?

I tried something like this but it finds nothing...

override func viewDidLoad()
    let imageArray = NSBundle.mainBundle().URLsForResourcesWithExtension(
          "png", subdirectory: "images")
    for n:NSURL in imageArray!
        { print("found ..." ,n) }

We we assume the images are in the app's resource bundle. If not you need to make sure that your images directory is listed in the "Copy Bundle Resources" in the "Build Phases" of the target.

EDIT This is only going copy the images into the app bundle, if you require the folder to be copied to the app bundle per the code below then please use the follow StackOverflow question to set it up correctly.

This gives us an array of URL's that we can then use with UIImage(data:) and NSData(contentsOfURL:) to create the image when needed.

Get the bundle's resource path and append the image directory then get the contents of the directory.

     if let path = NSBundle.mainBundle().resourcePath {

        let imagePath = path + "/images"
        let url = NSURL(fileURLWithPath: imagePath)
        let fileManager = NSFileManager.defaultManager()

        let properties = [NSURLLocalizedNameKey,
                          NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]

        do {
            let imageURLs = try fileManager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: properties, options:NSDirectoryEnumerationOptions.SkipsHiddenFiles)

            print("image URLs: \(imageURLs)")
            // Create image from URL
            var myImage =  UIImage(data: NSData(contentsOfURL: imageURLs[0])!)

        } catch let error1 as NSError {

How to get array of UIImage, from folder, in Swift?, (Or even, an array of the image names or similar, that would be fine – then could load them UIImage(named: ). How do I get at that folder  UIImage array in Swift. I am trying to create an array of images, then randomly select one; this is my code so far: Were I have the array type specified and the


Please try as followings:

My current situation was as follows. enter image description here

  1. You have to register your images to "Copy Bundle Resources".

  2. You have to add filter module in main Bundle. enter image description here

It is working as well on my side. Maybe you can change filter from "jpg" format into "png" one.

I've tested on iOS 10.x later, Swift 3.0 and xcode 8.1 version. If you have any problem, please contact me.

UIImage array in Swift., (Or even, an array of the image names or similar, that would be fine - then could load them UIImage(named: ). How do I get at that folder  Instead, UIImage is the data type you'll use to load image data, such as PNG or JPEGs. When you create a UIImage, it takes a parameter called named that lets you specify the name of the image to load. UIImage then looks for this filename in your app's bundle, and loads it.

Swift 4

    if let path = Bundle.main.resourcePath {
        let imagePath = path + "/images"
        let url = NSURL(fileURLWithPath: imagePath)
        let fileManager = FileManager.default

        let properties = [URLResourceKey.localizedNameKey,

        do {
            let imageURLs = try fileManager.contentsOfDirectory(at: url as URL, includingPropertiesForKeys: properties, options:FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)

            print("image URLs: \(imageURLs)")
            // Create image from URL
            let firstImageURL = imageURLs[0]
            let firstImageData = try Data(contentsOf: firstImageURL)
            let firstImage = UIImage(data: firstImageData)

            // Do something with first image

        } catch let error as NSError {

How to Programmatically Save and Load UIImage Files in The , func randomNumber() -> UIImage { var unsignedArrayCount 'diceImages'". Is it because I don't have the image file path in the array yet? The method above will first fetch all the content inside the ImagePicker folder. The contentsOfDirectoryAtPath method will return an array of strings reflecting the files’ names. Then, you just loop over the images, extract them as NSData types, transform to UIImage types then store to the images array.

I would advise against loading all of your images into an array at once. Images tend to be large and it's easy to run out of memory and crash.

Unless you absolutely have to have all the images in memory at once it's better to keep an array of paths or URLs and load the images one at a time as needed.

Assuming the folder full of images is in your app bundle, you can use the NSBundle method URLsForResourcesWithExtension:subdirectory: to get an array of NSURLs to all the files in your images subdirectory, either with a specific filetype, or ALL files (if you pass nil for the extension.)

Once you have an array of file urls you can map it to an array of paths if needed, and then map that to an array of images.

UIImage, How to save and load files from a directory with a given path. Also, select the ViewController.swift file from the Project navigator view to load it in the The images and titles arrays are just where will all images be saved  Other methods of the UIImage class let you create animations from specific types of data, such as Core Graphics images or image data that you create yourself. UIKit also provides the UIGraphics Get Image From Current Image Context() function for creating images from content that you draw yourself. You use that function in conjunction with a

Loading images with UIImage, iOS 2.0+; Mac Catalyst 13.0+; tvOS 9.0+; watchOS 2.0+ When creating image objects using the methods of this class, you must have existing image data  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Save And Get Image From Document Directory, Load the detail view controller layout from our storyboard. Set its selectedImage property to be the correct item from the pictures array. Show the new view  When you want to use a variable with yet unknown value, it's common to specify optional type explicitly. For your case: <code> var image: UIImage? </code>; In which case it gets set to nil automatically.

Array of photos - best way of accessing? : swift, Enter Swift as Language and choose Next. 3. Step 2 :- Add Any Image In Project. How to add UIImageView programmatically in swift. copy it to your project folder. Best way to do this is open the Project navigator of Xcode (If it is not already