What can i do whether add images to assets or in folder in ios What can i do whether add images to assets or in folder in ios xcode xcode

What can i do whether add images to assets or in folder in ios


Obviously the best apporach is to add images in xcassets. Import images by clicking on the blue folder called "Images.xcassets" then click on the small "+" plus sign at the bottom of the window that appears. Now choose "Import" to put images in there.It's really helpful because you'll only see 1 image name instead of duplicate names with extensions like "@2x" and "@3x".

enter image description here

Why this approach benefits

This is the best way you can organize images. The concept of App slicing applies here.

Refer, in case if you have no idea about what app thinning concept is:

App thinning appcoda

How do I need to load images

You can take those image names in JSON file read from that (Recommended). Or else take an array and put all your image names into that and load (Not recommended).

Use below code in case if you are reading from JSON file

 if let path = Bundle.main.path(forResource: "assets/test", ofType: "json") {         let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)        let jsonObj = JSON(data: data)}


the best and simple approach is to add all images in Assets.catalog .Now in you ViewController Create a Array of Strings [String] containing name of all images.Now using for-in loop access each element of and print name and image.Now creating programmically label and image and whenever xPosition exceeds width of Screen

SWIFT example

     let imageArray:[String] = ["image1","image2","image3","image4"]        let xPosition = 20        let yPosition = 20        for item in imageArray{         let imageView = UIImageView(image: UIImage(named: item)!)          if (xPosition < (self.view.frame.width - 200)){            imageView.frame = CGRect(x: xPosition, y: yPosition, width: 80, height: 80)            var label = UILabel(frame: CGRectMake(xPosition,yPosition + 90, 20, 20))            label.text = item            xPosition += 100          }else{           yPosition += 100           xPosition = 20           imageView.frame = CGRect(x: xPosition, y: yPosition, width: 80, height: 80)              var label = UILabel(frame: CGRectMake(xPosition, yPosition + 90, 20, 20))           label.text = item          }         self.view.addSubview(imageView)         self.view.addSubview(label)         }        }