0

I am working on a TVOS 10 project with Swift 3.0 and I am trying to access the files in the Assets folder from the controller.

I have this array:

var posters: [String] = ["image1", "image2", "image3","image4", "image5"]

But I want to populate this array programmatically. So if i change the files in the assets, the content of the array should change.

Because later I do this:

//currentImage is an ImageView in storyboard
 currentImage.image = UIImage(named: posters[currentPoster])

This is the hierarchy of my files.enter image description here

This is what I have tried but it does not give me what I want.

if let resourcePath = Bundle.main().resourcePath {
        print(resourcePath)
        do {
            let temp = try FileManager.default().contentsOfDirectory(atPath: resourcePath)
            print(temp)
            let assetsPath = resourcePath + "/" + temp[6]
            let temp2 = try FileManager.default().contentsOfDirectory(atPath: assetsPath)
            print (temp2)
        }
        catch let error as NSError {
            print(error)
        }
    }

Output:

/var/containers/Bundle/Application/9AECCDB0-DD5F-4377-9237-6E7DA1E14A39/PosterAppTV.app

["Assets.car", "Base.lproj", "Frameworks", "Info.plist", "META-INF", "PkgInfo", "PosterAppTV", "_CodeSignature", "embedded.mobileprovision", "libswiftRemoteMirror.dylib"]

Error Domain=NSCocoaErrorDomain Code=256 "The file “PosterAppTV” couldn’t be opened." UserInfo={NSFilePath=/var/containers/Bundle/Application/9AECCDB0-DD5F-4377-9237-6E7DA1E14A39/PosterAppTV.app/PosterAppTV, NSUserStringVariant=( Folder ), NSUnderlyingError=0x1740524e0 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}

Any help how to get a list of names of the files in the Assets/Posters folder? Also, if this may not be possible, are there other ways to store some pictures into a folder and then access the names of those files programmatically?

user2512806
  • 421
  • 1
  • 9
  • 26
  • Related iOS post (it works the same for tvOS): [Access Asset Catalog pathForResource](http://stackoverflow.com/questions/18968352/access-asset-catalog-pathforresource). Not sure you can programmatically reach into the asset catalog, as it is compiled at runtime. – JAL Jul 11 '16 at 21:05
  • That post basically says you can really do it programatically. – user2512806 Jul 11 '16 at 21:17
  • I want to do something: var allFileInAsset = getFilesInAssets(): for(file in allFilesInAssets) {do something with file.name} – user2512806 Jul 11 '16 at 21:17
  • @JAL I updated the question. See if it makes more sense what i am trying to do. – user2512806 Jul 11 '16 at 21:27
  • @user2512806 checkout my answer to your question https://stackoverflow.com/questions/38316068/access-the-files-folders-in-the-assets-folder-for-tvos-programmatically/52361102#52361102 – Xcodian Solangi Sep 18 '18 at 07:23

1 Answers1

0

When you are placing your images in assets it means xcode have remembered your all image names, there is no need to call any folder name or directory path, now you can directly call your image name in code.

Here is what you are looking for:

var posters: [String] = ["image1", "image2", "image3","image4", "image5"]

if let myImage = UIImage(named: posters[2]) {
         imageView.image = myImage  //image3 is called at the index of 2 in posters array
   } 
 else {
      print("image not found")
 }

Or you can retrieve your images in one line like this:

imageView.image = UIImage(named: posters[2]) 
Xcodian Solangi
  • 2,342
  • 5
  • 24
  • 52