简体   繁体   中英

Swift 4 add images to image array

I have this code:

var imgArr: [UIImage?] = []
imgArr = [UIImage(named: "a1.jpg"), UIImage(named: "a2.jpg"), UIImage(named: "a3.jpg"), UIImage(named: "a4.jpg"), UIImage(named: "a5.jpg")] // this is files in assets



let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            let path = documentsDir.appendingPathComponent((AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/json/myPackageData.json") // this is json file in documents folder
            do {
                let data = try Data(contentsOf: path)
                let decoder = JSONDecoder()
                let jsonData = try decoder.decode(MyPackageData.self, from: data)

                for image in jsonData.tipsSlidesSizes.keys {
                    print("<!= \(image)")
                    imgArr.append(UIImage(named: image))
                }


            } catch {
                print("ConceptGalleryView - Error 1030: Problem with parse file. \(error)")
            }

            print("Summary:")
            debugPrint(imgArr)

I have photo in path: Documents/myimages/......

as a result of this code I get:

Summary:
[Optional(<UIImage: 0x6000000b1d60>, {1000, 750}), Optional(<UIImage: 0x60c0000ae580>, {600, 630}), Optional(<UIImage: 0x60c0000aeb80>, {1537, 1080}), Optional(<UIImage: 0x60c0000aedc0>, {1035, 1608}), Optional(<UIImage: 0x6080000ae5e0>, {1280, 853}), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

And the files are not visible later in the slider.

Why can not I see in the table of my photos? What's wrong with this code?

UPDATE CODE

OK, I'm trying update my code with yours suggestion:

let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            let path = documentsDir.appendingPathComponent((AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/json/myPackageData.json")
            do {
                let data = try Data(contentsOf: path)
                let decoder = JSONDecoder()
                let jsonData = try decoder.decode(MyPackageData.self, from: data)

                for image in jsonData.tipsSlidesSizes.keys {
                    print("<!= \(image)")
                    imgArr.append(UIImage(named: image))
                    let imgPath = documentsDir.appendingPathComponent((AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/GET_TIPS_SLIDES/")

                    let dataImg = NSData(contentsOf: URL(string: imgPath.path)!)
                    if let img = UIImage(data: dataImg) {
                        imgArr.append(UIImage(named: img))
                    }

                }


            } catch {
                print("ConceptGalleryView - Error 1030: Problem with parse file. \(error)")
            }

but I have error in:

if let img = UIImage(data: dataImg) {

Cannot convert value of type 'NSData?' to expected argument type 'Data'

You have to get NSData from your DocumentDirectory path , after that convert in to your image :

if let dataImg = NSData(contentsOf: URL(string: "")!) {
      if let img = UIImage(data: dataImg as Data) {

      }
}

where imgPath : your DocumentDirectory image path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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