简体   繁体   English

如何在核心数据中按名称保存图像?

[英]How to save image by its name in core data?

I have images in Assets.xcassets named city_00,city_01,...,city_10 我在Assets.xcassets中有名为city_00,city_01,...,city_10

I stored them in an array 我将它们存储在数组中

 let cityImages: [UIImage] = [UIImage(named: "city_00")!,UIImage(named: "city_01")!,.....UIImage(named: "city_10")!]

 let cityNames = ["City A","City B",....., "City J"]

Then I call them using collectionview in my project and allow users to select any city they like 然后我在项目中使用collectionview对其进行呼叫,并允许用户选择他们喜欢的任何城市

 var imageSelected = [UIImage]()
 var cityImage:UIImage?
var cityImageName:String?

   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        collectionView.allowsMultipleSelection = false
        cityImage = imageSelected[(indexPath as NSIndexPath).row]
 let selectedImgName = String(format: "%%" ,imageSelected[(indexPath as NSIndexPath).row])
        cityImageName = selectedImgName
}

How do I store the name of city in cityImageName based on image selected? 如何根据所选图像在cityImageName存储城市名称?

You should probably use NSValueTransformer to convert your image to NSData and then store the data blob in Core Data. 您可能应该使用NSValueTransformer将图像转换为NSData,然后将数据blob存储在Core Data中。 Here is a tutorial that shows how to accomplish this. 是一个显示如何完成此操作的教程。

If you really want to store it as string (it will take more space because of the encoding) you can have a look at this question on how to base64 encode an image. 如果你真的想将其存储为字符串(它会采取更多的是因为编码的空间),你可以看看这个问题,就如何为base64的图像进行编码。

It is not advisable to store images into core data. 不建议将图像存储到核心数据中。 This will lack app in performance. 这将缺乏应用程序的性能。

Converting Image into Base64 string and convert Base64 String into image will take plenty of time. 将Image转换为Base64字符串并将Base64字符串转换为image将花费大量时间。

I would advise you to Store your image into document folder and store that path into your coreData. 我建议您将图像存储到文档文件夹中,并将该路径存储到coreData中。

This way you can store and retrieve image easily with so much better app performance then storing image into Core Data. 这样一来,您可以轻松存储和检索图像,并具有比将图像存储到Core Data更好的应用程序性能。

You can use lazy loading as well with this approach will improve app performance further. 您也可以通过这种方式使用延迟加载,这将进一步提高应用程序的性能。

As you have stated you have all image in your image asset then just store name of your image in Core data base. 如前所述,图像资产中已包含所有图像,然后只需将图像名称存储在Core数据库中即可。

You have image_name stored in cityImages array. 您有image_name存储在cityImages数组中。

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

     let yourRequiredImage:String = cityImages[indexPath.row]// Save this Image name in your core data.
     print (yourRequiredImage)
}

Your cityImages array is an array of images. 您的cityImages数组是一个图像数组。

Change that to an array of image names instead. 改为将其更改为图像名称数组。

It will use a lot less memory and then in the selected method you can get the image name from the array at the selected index. 它将使用更少的内存,然后在选定的方法中,您可以从数组的选定索引处获取图像名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM