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

前端 未结 5 1414
别跟我提以往
别跟我提以往 2021-02-13 19:18

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\" im

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-13 19:57

    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,
                              URLResourceKey.creationDateKey, 
                              URLResourceKey.localizedTypeDescriptionKey]
    
            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 {
                print(error.description)
            }
        }
    

提交回复
热议问题