简体   繁体   English

以编程方式访问Assets文件夹中用于TVOS的文件/文件夹

[英]Access the files/folders in the Assets folder for TVOS programmatically

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. 我正在使用Swift 3.0进行TVOS 10项目,并且试图从控制器访问Assets文件夹中的文件。

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. 这是我文件的层次结构。 在此处输入图片说明

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 /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"] [“ 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." 错误域= NSCocoaErrorDomain代码= 256“无法打开文件“ PosterAppTV”。 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"}} UserInfo = {NSFilePath = / var / containers / Bundle / Application / 9AECCDB0-DD5F-4377-9237-6E7DA1E14A39 / PosterAppTV.app / PosterAppTV,NSUserStringVariant =(Folder),NSUnderlyingError = 0x1740524e0 {Error Domain = NSPOSIXErrorDomain Code = 20“ Not目录”}}

Any help how to get a list of names of the files in the Assets/Posters folder? 对如何获取Assets / Posters文件夹中文件名称的列表有帮助吗? 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? 另外,如果这不可能,是否还有其他方法可以将一些图片存储到文件夹中,然后以编程方式访问这些文件的名称?

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. 当您将图像放置在assets这意味着xcode记住了所有图像名称,无需调用任何文件夹名称或目录路径,现在您可以直接在代码中调用图像名称。

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]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM