我在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]
}
我喜欢通过将所选图像转换回字符串来将其保存在数据库中。我该怎么做?
答案 0 :(得分:1)
let cityImageData = UIImagePNGRepresentation(cityImage)
let imageString = cityImageData?.base64EncodedString()