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)?
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.
You can't save directly
image
into Realm, convert it intoData
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)
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.