我想问是否有方法将图像添加到我的资源组中的文件夹中的单元格。如何将图像添加到项目中文件夹的collectionview?
我有很多类别..对于每个类别:“Barvy”,“Budovy”里面有.jpg图像。
我需要的CollectionView这个群体的名字后,我单击单元格,我需要显示所有图片文件夹中,例如..我的应用程序的负载,我可以看到4个单元,每一个标题为: Barvy,Budovy,Bytove_doplnky,Casove_pasmo ..和我点击单元格后它将加载文件夹中的图像
我想问是否有方法将图像添加到我的资源组中的文件夹中的单元格。如何将图像添加到项目中文件夹的collectionview?
我有很多类别..对于每个类别:“Barvy”,“Budovy”里面有.jpg图像。
我需要的CollectionView这个群体的名字后,我单击单元格,我需要显示所有图片文件夹中,例如..我的应用程序的负载,我可以看到4个单元,每一个标题为: Barvy,Budovy,Bytove_doplnky,Casove_pasmo ..和我点击单元格后它将加载文件夹中的图像
这是我如何解决显示图片来自前两个组
我写了一个json文件并将其添加到我的项目
{
"Barvy": [ "Bila.jpg", "Cerna.jpg", "Cervena.jpg",
"Fialova.jpg", "Hneda.jpg", "Modra.jpg",
"Oranzova.jpg", "Zelena.jpg", "Zluta.jpg"],
"Budovy": [ "Budova.jpg", "Cirkus.jpg", "Domov.jpg",
"Dum.jpg", "Kostel.jpg", "Nemocnice.jpg",
"Obchod.jpg", "Restaurace.jpg", "Skola.jpg"]
}
然后
var arrImages = [String]()
let dataUrl = Bundle.main.url(forResource: "test", withExtension: "json")
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
let jsonData = try! Data(contentsOf: dataUrl!)
do {
if let arrJson = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
as? Dictionary<String, AnyObject> {
if let barvy = arrJson["Barvy"] as? [String] {
arrImages = barvy
}
if let budovy = arrJson["Budovy"] as? [String] {
arrImages += budovy
}
}
} catch {
print(error)
}
}
而且
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", for: indexPath) as? Cell {
cell.imgView.image = UIImage(named: "\(arrImages[indexPath.row])")
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrImages.count
}
最后从第2组我的图片显示在的CollectionView .. 当我弄清楚如何第一只显示我会更新答案类别的名称和点击仅用于点击类别的显示图像。
首先将所有组的图像重新命名为'Barvy_0.jpg'等。
然后声明一个空白字符串,如:
var image_name:String = ""
然后设置IMAGE_NAME作为按钮组名称点击像Barvy按钮点击:
image_name = "Barvy"
在的CollectionView:
cell.imageview.image = UIImage(named:"\(image_name)\(indexPath.row).png")!
你捆绑文件中的图像,因此您可以直接使用你的形象图像名称如[UIImage imagedNamed : @"<name.png>"]
。您有包含多个图像的文件夹,以便显示您可以执行的这些图像:
制作数组,其中包含此图像的名称,如NSArray *arrBravy = @[@"Bravy_cool.png", @"Bravy_angry.png" ... ];
用于您所有的四个文件夹。而在的CollectionView
cell.imgView.image = arrImages[indexPath.row];
arrImages = arrBravy or arrBudovy... etc , as per the condition user clicked on which option.
但正如你说你有很多文件夹,像这样,你不能管理多个阵列,来解决这个问题,你做一个JSON文件,并将其添加在你捆绑。现在在这个json文件上应用序列化,并获得所有文件的名称列表。对于JSON文件
建议图案:
{
{
"folderName": "Bravy",
"imgList": [ "Bravy_cool", "Bravy_angry", "Bravy_OK"]
},
{
"folderName": "Budovy",
"imgList": [ "Budovy_kiss", "Budovy_sleep", "Budovy_cry"]
}
}
我刚才给你例子有两个文件夹的名称,就可以高达您的使用扩展此。保持json是处理这类问题的最好方法。
如何用JSON DEAL
获取JSON数据:
NSArray *arrJson = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
分配Json的图像
//userSelectedString is string which notifes user selected which folder
for(int i= 0; i< arrJson.count: i++){
if(arrJson[i][@"folderName"] == @"userSelectedString"){
arrImages = arrJson[i][@"imgList"];
}
}
在的CollectionView ItemFor行
cell.imgView.image = arrImage[indexPath.row];
如果你有XML,没关系!你可以像处理json数据一样处理xml,但有点不同。
如果你想获取XML文件本教程中去:https://www.raywenderlich.com/725/xml-tutorial-for-ios-how-to-read-and-write-xml-documents-with-gdataxml
如果你想转换XML到JSON去与这个在线转换器和下载你的文件:http://www.utilities-online.info/xmltojson/#.WFTDfuZ97IU
如何在UICOLLECTIONVIEW中显示图像
先写这
@interface myViewController : UIViewController {
//BOOL isFolderSelected;
NSMutableArray *arrMutImages;
}
在ViewDidLoad
arrMutImages = [NSMutableArray New];
[arrMutImages addObject:[self getFolderNames]];
做出新的方法
- (NSMutableArray *) getFolderNames{
NSMutableArray *arrMutfetchImage = [NSMutableArray New];
for(int i=0 ; i < arrJson.count ; i++){
[arrMutfetchImage addObject:arrayJson[i][@"folderName"]]
}
return arrMutfetchImage;
}
在numberOfItemsInSection
return arrMutImages.count;
在cellforrowatindexpath
imageView.image = [UIImage imageNamed: arrMutImages[indexPath.row]];
在
didSelectItemAtIndexPath
[arrMutImages removeAllObjects];
[arrMutImages addObject:arrayJson[indexPath.row][@"ImgList"]];
[collectionView reloadData];
如果从图像列表任何按钮切换到文件夹,然后在按钮动作下面写代码
[arrMutImages removeAllObjects];
[arrMutImages addObject:[self getFolderNames]];
[collectionView reloadData];
非常感谢!我刚刚为每个组获得了一个XML文件..有什么方法可以使用这些XML而不是JSON?或者我可以使用一些在线转换器从XML到JSON – deniskakacka
我添加了答案,我如何解决它,但如果您可以帮助“如何首先显示类别的名称,然后单击显示图像仅用于点击类别”。随时回答:) @Tinu Dahiya – deniskakacka
@deniskakacka检查我的新更新答案。我现在没有xcode或mac,所以我只是把这段代码写在记事本中并在这里发布。我尽力而为,完整的解决方案几乎在这里。所以请检查你的输出是什么,如果不合适,请告诉我。 –
是的但在每个文件夹里面有很多其他的图像我只是不能重命名它们..有没有另一种方式? – deniskakacka
那么你应该为每个文件夹的图像制作数组 –