Reputation: 902
I have a requirement for an editor app where i want to store data in the form of image ,audio and video in realm Object as i am new to realm and swift.where i am able to write data but how to read in form of array of objects.
Upvotes: 0
Views: 643
Reputation: 16021
General best practice for Realm is to try and avoid saving large binary blobs to Realm; especially if they can simply be saved on the hard drive next to the Realm file. If you need a Realm Object
to represent the file, you can store the file path to the file in Realm as a String
property.
If you still have a specific need to write a UIImage
to a Realm object, it's necessary to convert it to NSData first so it can be saved to disk. This usually means converting it to a JPEG or PNG.
// Write a UIImage as a PNG to Realm
let myImage: UIImage = ...
let myImageData = UIImagePNGRepresentation(myImage) as NSData?
if let myImageData = myImageData {
// A Realm `Object` with an `NSData`
let myObject = MyObject() property
myObject.imageData = myImageData
let realm = try! Realm()
try! realm.write {
realm.add(myObject)
}
}
// Get a UIImage from Realm
let realm = try! Realm()
let myObject = realm.objects(MyObject.self).first!
let myImage = UIImage(data: myObject.imageData)
Upvotes: 1