Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

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.

like image 200
Amit Kumar Avatar asked Jan 05 '17 12:01

Amit Kumar


1 Answers

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)
}
like image 185
Sivajee Battina Avatar answered Oct 18 '22 16:10

Sivajee Battina