Amit Kumar
Amit Kumar

Reputation: 603

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

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.

Upvotes: 9

Views: 9106

Answers (2)

Jagdeep
Jagdeep

Reputation: 1178

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

Upvotes: 0

Sivajee Battina
Sivajee Battina

Reputation: 4174

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

Upvotes: 7

Related Questions