Does SwiftData support UIImage

Does SwiftData support UIImage as in CoreData specified here: https://www.swiftdevjournal.com/saving-images-in-core-data/ If it does, how to specify that in the @Model schema, especially using external storage to save the image in a separate file. Thanks.

Post not yet marked as solved Up vote post of youngn3 Down vote post of youngn3
2.1k views

Replies

I converted my Core Data app to SwiftData and here is how the @Model turned out with respect to my image.

@Attribute(.externalStorage) var imageData: Data?

  • What if it is an array of images? It shows me error when I try to store more than an image

  • Are you trying to save an Arrsy of data?

Add a Comment

Thanks @SpaceMan!

Here's how I turned my data into a UIImage.

var myImage: UIImage? = nil
if let imageData = modelEntity.imageData {
    myImage = UIImage(data: imageData)
}
  • Has the issue resolved? I convert a UIimage to data by the following codes, and save it to swiftData. But I get nil when I read it. Why? //save photo with swiftData let cameraPhotoID = UUID().uuidString let image = camera.photo.image! let data = image.pngData() let CameraPhoto = cameraPhoto(cameraPhotoID: cameraPhotoID,cameraPhotoData: data!) modelContext.insert(CameraPhoto) try? modelContext.save()

Add a Comment