5

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 according to resize the picked image. I googled and also saw here but not clear well. If images are small in size or less in numbers or if I have more images or large size what can be done?

another confusion is what I will use either NSData or NSString ? my model is

class IndividualContact: Object {

    dynamic var photoDataString = ""
    var photoData: NSData? = nil
    dynamic var isPhotoAvailable = false
}

and I already picked image from gallery

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

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

            imageView.contentMode = .ScaleAspectFit
            imageView.image = pickedImage 
        }

now how can I store it according to resize it as (120*110 size)?

Community
  • 1
  • 1
KhanShaheb
  • 714
  • 2
  • 11
  • 26
  • Whats you question? resize image ya store image in database – Chandresh Kachariya Oct 05 '16 at 06:37
  • I suggest to copy the image to the "Documents" directory and store its path. – Ahmad F Oct 05 '16 at 06:38
  • Store image directly on realm is not advised, you should just store the image on phone's document directory and store image name/path on Realm – Tj3n Oct 05 '16 at 06:41
  • 1
    If I am not mistaking, you can find the answer [here](http://stackoverflow.com/questions/33299523/how-to-put-an-image-in-a-realm-database). – Ahmad F Oct 05 '16 at 06:42
  • i don't need downloading from net. I need to resize it and store it to Realm Database. Also I wanted to know about "if I have more images or large size" – KhanShaheb Oct 05 '16 at 06:55

2 Answers2

7

You can not save directly image into Realm, convert it into NSData and the store it.

if your image is of type JPEG then use,

let data = NSData(data: UIImageJPEGRepresentation(yourImage,0.9))

and if your image is of PNG type then use,

let data = NSData(data: UIImagePNGRepresentation(yourImage))

after that write you file in realm like,

let realm = Realm()
realm.write{
    realm.add(data)
}

Hope this helps.

Sachin Amrale
  • 277
  • 1
  • 9
  • if I dont know what type of image it is then what I will do? – KhanShaheb Oct 05 '16 at 08:45
  • you can directly give a file path or url to get the NSData – Sachin Amrale Oct 05 '16 at 08:56
  • @IrtizaKhanNiloy If we want to store image we have to convert that image into a data and then we are able to store that image, so to convert image into data there are many ways like if you want to store image wich comes from url then you can use let data = NSData(contentsOfURL:NSURL(string: "your image url")) – Sachin Amrale Oct 05 '16 at 09:21
  • Brother I don't need url, I need to store an image from gallery. – KhanShaheb Oct 05 '16 at 09:30
  • You can get the image using imagePickerController:didFinishPickingImage: method and default image type of images which is captured using camera is JPEG so you can easilly convert it into NSData. – Sachin Amrale Oct 05 '16 at 09:50
  • I did but now I failed to retrieve data from database. – KhanShaheb Oct 05 '16 at 10:04
3

You can't save directly image into Realm, convert it into Data and the store it.

Convert your image to Data

let data = NSData(data: UIImageJPEGRepresentation(yourImage,0.9))

Convert data to PNG format if any

let imgPNG = UIImage.imageWithData(data)

Convert PNG image to Data

let dataPNGImg = NSData(data: UIImagePNGRepresentation(imgPNG))

Store your data into Realm DB...

  let realm = Realm()
    realm.write{
        realm.add(dataPNGImg)
    }

IMPORTANT!!!

It is not a good idea to store an image in the database. It will increase database load and processing time. You can store the image path instead of the image.

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65