如何将选中的图像转换为字符串进行存储?

时间:2016-12-08 11:10:47

标签: ios swift core-data

我在Assets.xcassets中有名为city_00,city_01,...,city_10

的图片

我将它们存储在一个数组

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

然后我在项目中使用collectionview给他们打电话,让用户选择他们喜欢的任何城市

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

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

我喜欢通过将所选图像转换回字符串来将其保存在数据库中。我该怎么做?

1 个答案:

答案 0 :(得分:1)

let cityImageData = UIImagePNGRepresentation(cityImage)
let imageString = cityImageData?.base64EncodedString()