xcode 无论是将图像添加到资产还是在 ios 中的文件夹中,我都可以做什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41485128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What can i do whether add images to assets or in folder in ios
提问by Amit Kumar
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.
我正在构建一个 ios 应用程序,其中我有许多图像要显示在许多 ViewController 的 ImageView 中。这就是为什么我必须向项目添加许多图像。
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.
是否可以将所有图像添加到项目的文件夹中并获取这些图像并在 ImageViews 中显示它们及其在 UILabels 中的名称?如果可能,请告诉我执行此操作的方法。
回答by Sivajee Battina
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".
显然,最好的方法是在 xcassets 中添加图像。通过单击名为“Images.xcassets”的蓝色文件夹导入图像,然后单击出现的窗口底部的小“+”加号。现在选择“导入”将图像放入其中。这真的很有帮助,因为您只会看到 1 个图像名称,而不是带有“@2x”和“@3x”等扩展名的重复名称。
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:
如果您不知道应用程序精简概念是什么,请参考:
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).
您可以在 JSON 文件中读取这些图像名称(推荐)。或者采用一个数组并将所有图像名称放入其中并加载(不推荐)。
Use below code in case if you are reading from JSON file
如果您正在读取 JSON 文件,请使用以下代码
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)
}
回答by Jagdeep
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-inloop access each element of and print name and image.Now creating programmically label and image and whenever xPosition exceeds width of Screen
最好和最简单的方法是在 Assets.catalog 中添加所有图像。现在在你的 ViewController 创建一个包含所有图像名称的字符串数组[String]。现在使用for-in循环访问每个元素并打印名称和图像。现在以编程方式创建标签和图像,并且每当 xPosition 超过屏幕宽度时
SWIFT example
SWIFT 示例
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)
}
}