9

I'm building an ios app in which i have many images to show in many ViewController's ImageView. Thats why i have to add many images to the project.

Please tell me the best practice to add images in project. Whether i add in assets or in some other folder of my project.

If adding in some folder is best approach then how can i access those images.

Is this possible to add all images in a folder of the project and fetch those images and show in ImageViews and their name in UILabels one by one? If it is possible then please tell me the way to do this.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Amit Kumar
  • 583
  • 5
  • 16
  • Why don't you use the asset catalog?! – Gulliva Jan 05 '17 at 12:33
  • Add all image in asset – Narendra Pandey Jan 05 '17 at 12:34
  • 1
    1. Best Practice to use `Assets` 2. If you want to fetch images from a folder then add all in a folder and use NSFileManager to fetch all images from specific folder see my [answer](http://stackoverflow.com/a/24390080/790842) to know how to create folder – iphonic Jan 05 '17 at 12:35
  • hey, I have seen your another answer but swift is saying that there is no method "stringByAppendingPathComponent" in bundle and it is not properly convey me what to do. Would you please one more time in recent swift code. I'm hoping your answer – Amit Kumar Jan 06 '17 at 20:37

2 Answers2

7

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)
}
Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
0

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)
         }
        }
Jagdeep
  • 1,158
  • 11
  • 16