0

I have images in Assets.xcassets named 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

 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?

leaner122
  • 647
  • 9
  • 21
  • Both `cityImages` and `cityNames` have equal objects count? – Nirav D Dec 08 '16 at 16:04
  • Yes they have equal count – leaner122 Dec 08 '16 at 16:07
  • If you have created `cityImages` array then why are you using `imageSelected` array inside `didSelect` method? – Nirav D Dec 08 '16 at 16:10
  • That is because I am using imageSelected variable while segueing images into different viewcontorller – leaner122 Dec 08 '16 at 16:21
  • 1
    What are you doing wrong? Saving an image as a string, for one thing. Why would you do that? – Tom Harrington Dec 08 '16 at 21:32
  • @TomHarrington, yeah now thinking about it, after you mentioned I was not thinking at all, I was just thinking at that time string requires less space than image. I am editing my question. I think I will just go with saving image name. – leaner122 Dec 08 '16 at 22:22
  • If the images are bundled in the app why are you saving them to core data? They are already there. No need to save them again. – Fogmeister Dec 10 '16 at 12:26
  • @Fogmeister, I just want to store name of image selected, so that I can display them later in another viewController but this says http://stackoverflow.com/questions/28515845/swift-how-to-get-currently-displayed-image-file-name-from-uiimageview it is not possible. Can you point me towards any solution? – leaner122 Dec 11 '16 at 22:58
  • @leaner122 ah. What I said in that other question is that it isn't possible to get the name of the image directly from there image view (or even the image itself). There's is nothing stopping you from storing the required name in core data. Instead of creating two arrays just create one array using a struct or something similar. – Fogmeister Dec 11 '16 at 23:32
  • If you want to store a string in core data just create an entity with a string attribute. Im not sure what you're asking as this is fairly straight forward in core data. Let me know where you're stuck and I'll try to help. – Fogmeister Dec 11 '16 at 23:38
  • @Fogmeister, I apologize if I misread your answer, I am new to swift, I am not able to save image name into cityImageName variable. I edited question above, thanks – leaner122 Dec 12 '16 at 00:46

3 Answers3

0

You should probably use NSValueTransformer to convert your image to NSData and then store the data blob in 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.

Community
  • 1
  • 1
Rad'Val
  • 8,895
  • 9
  • 62
  • 92
  • All my images are presents in Assets.xcassets and I am not saving anything from phone, so is it necessary to store it as a data blob? I already looked at that question and that is how I encoded it to string as shown in the question. – leaner122 Dec 08 '16 at 15:32
  • 1
    If all the images are already in the app, why don't you just save the name? – James P Dec 08 '16 at 15:39
  • @JamesP This says, it is not possible to store name stackoverflow.com/questions/28515845/…. Do you have any alternative method? – leaner122 Dec 11 '16 at 23:07
0

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.

I would advise you to Store your image into document folder and store that path into your coreData.

This way you can store and retrieve image easily with so much better app performance then storing image into 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.

You have image_name stored in cityImages array.

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

     let yourRequiredImage:String = cityImages[indexPath.row]// Save this Image name in your core data.
     print (yourRequiredImage)
}
Devang Tandel
  • 2,988
  • 1
  • 21
  • 43
  • This says, it is not possible to store name http://stackoverflow.com/questions/28515845/swift-how-to-get-currently-displayed-image-file-name-from-uiimageview. Do you have any alternative method? – leaner122 Dec 11 '16 at 22:55
  • 2
    @leaner122 please stop saying that I said it's not possible to store the name. The name is a string. Storing it in core data is trivial. What is not possible is to create a UIImage and then ask it for its image name. That is a completely different thing. – Fogmeister Dec 11 '16 at 23:55
  • 1
    @leaner the link you mentioned says it is not possible to store image from UIImageView. We already have name of image isn't it? Why do we need to fetch name from UIImageView? – Devang Tandel Dec 12 '16 at 05:12
0

Your cityImages array is an array of images.

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.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306