0

I'm writing an iOS application using Swift 2.2 and I want to save profile picture of an account locally in a Realm database.

I tried from here and at last I have an imageView,two buttons. one button is for save image and another for show.In my imageView I have picked image from gallery and taken NSData and also tried to store it in Database but I can not retrieve it. Please suggest any procedure so that I will able to store an image and also to show it.

my Model is

class ImageModel:Object{
   var photoData: NSData? = nil 
}

and in my Image class I did

var imgData = NSData()

my image picker controller looks like below

  func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

            imageView.contentMode = .ScaleAspectFit
            imageView.image = pickedImage

            let imageData = UIImagePNGRepresentation(pickedImage)!
            self.imgData = imageData
            //print(imgData,"\n")

        }

in my save button I did

 let imgModel = ImageModel()

@IBAction func SaveImageToDB(sender: AnyObject) {

    imgModel.photoData = self.imgData
    print(imgModel.photoData)

    try! moreUses.sharedInstance().realm.write{
        try! moreUses.sharedInstance().realm.add(imgModel)
    }
}
@IBAction func ShowImageFromDB(sender: AnyObject) {
    let lists = moreUses.sharedInstance().realm.objects(ImageModel)
    print(lists)

    //let image : UIImage = UIImage(data: lists[0].photoData!)!
    //print(image)


}

and my singleton class for realm object is

class moreUses{

    var realm = try! Realm()

    static var mrUser: moreUses!

    class func sharedInstance() -> moreUses {
        if mrUser != nil {
            return self.mrUser
        }
        return moreUses();
    }

    init() {
    }
}
Community
  • 1
  • 1
KhanShaheb
  • 714
  • 2
  • 11
  • 26

1 Answers1

1

Before answering, let me just say that it's best practice to avoid saving large data objects like images in Realm. Since those types of data can already be saved as normal files easily, there's no real advantages to storing them in Realm. It's far better to simply have a Realm Object that represents the image, but you simply access it on disk.

If you absolutely must save an image to Realm, then you were already on the right track:

let image = UIImage()
let imageData = UIImagePNGRepresentation(image)

let realm = try! Realm()

let imageObject = ImageObject()
imageObject.data = imageData

try! realm.write {
   try! realm.add(imageObject)
}

let queriedImageObject = realm.objects(ImageObject.self).first
let retrievedImage = UIImage(data: queriedImageObject.data)

To further review your code there:

  • It's not really necessary to place a Realm instance in a singleton. Each time you call Realm(), it will re-use the same copy on that thread, so there are no performance hits there.
  • You're referencing a single constant object of ImageModel() whenever you're adding an image to your database. Depending on your use case, you should either create a new instance of ImageModel() each time if you're creating a new copy each time, or try! realm.write { } to update the original object if you want to keep just one (Instead of calling realm.add() each time).
TiM
  • 15,812
  • 4
  • 51
  • 79
  • please see that I've two button function.one for save and another for restore. all u did in a single function and used ImageObject(). how can I restore from func ShowImageFromDB() – KhanShaheb Oct 06 '16 at 04:58
  • please see this code. when I simply tried to run your code it shows nil – KhanShaheb Oct 06 '16 at 07:16
  • Hmm, I just tried running my own set of that code and it worked fine. I'm not sure why that's happening. Are you able to inspect the contents of the Realm file to make sure the object is actually being saved where you're expecting it? – TiM Oct 07 '16 at 01:26
  • Sorry I am Unable to inspect the contents of the Realm file to make sure the object is actually being saved where I was expecting it. Can you suggest? – KhanShaheb Oct 08 '16 at 05:47