Save Image to Realm

I am trying to select an image from a Photo Library device in a method:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { userPhoto.image = info[UIImagePickerControllerOriginalImage] as! UIImage? userPhoto.contentMode = .scaleAspectFill userPhoto.clipsToBounds = true dismiss(animated: true, completion: nil) } 

and save this image in Realm (as NSData):

 asset.assetImage = UIImagePNGRepresentation(userPhoto.image!)! as NSData? 

...

  try! myRealm.write { user.assetsList.append(asset) myRealm.add(user) } 

After the assembly, I managed to try and select and save the image (in the application). I have an application error: 'Binary too big'

What am I doing wrong?

PS Sorry for my English :)


After some action, I have this code. But he rewrites my image.

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL let imageName = imageUrl.lastPathComponent let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let photoURL = NSURL(fileURLWithPath: documentDirectory) let localPath = photoURL.appendingPathComponent(imageName!) let image = info[UIImagePickerControllerOriginalImage]as! UIImage let data = UIImagePNGRepresentation(image) do { try data?.write(to: localPath!, options: Data.WritingOptions.atomic) } catch { // Catch exception here and act accordingly } userPhoto.image = image userPhoto.contentMode = .scaleAspectFill userPhoto.clipsToBounds = true urlCatch = (localPath?.path)! self.dismiss(animated: true, completion: nil); } 
+5
source share
3 answers

Do not save the image directly in the area, just save the location of the image in the area as String or NSString and load the image from this saved path. Performance is always better to load images from this physical location, and your database does not get too big

  func loadImageFromPath(_ path: NSString) -> UIImage? { let image = UIImage(contentsOfFile: path as String) if image == nil { return UIImage() } else{ return image } } 

or just save the image name if it is in your document directory anyway

 func loadImageFromName(_ imgName: String) -> UIImage? { guard imgName.characters.count > 0 else { print("ERROR: No image name") return UIImage() } let imgPath = Utils.getDocumentsDirectory().appendingPathComponent(imgName) let image = ImageUtils.loadImageFromPath(imgPath as NSString) return image } 

and here is an example of how to save the captured image to your directory with a unique name:

  @IBAction func capture(_ sender: AnyObject) { let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo) stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (imageDataSampleBuffer, error) -> Void in let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) //self.stillImage = UIImage(data: imageData!) //self.savedImage.image = self.stillImage let timestampFilename = String(Int(Date().timeIntervalSince1970)) + "someName.png" let filenamePath = URL(fileReferenceLiteralResourceName: getDocumentsDirectory().appendingPathComponent(timestampFilename)) let imgData = try! imageData?.write(to: filenamePath, options: []) }) /* helper get Document Directory */ class func getDocumentsDirectory() -> NSString { let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] //print("Path: \(documentsDirectory)") return documentsDirectory as NSString } 
+7
source

https://realm.io/docs/objc/latest/#current-limitations maximum data size is 16 MB. this is a limitation of scope

+1
source

Depending on how you serialize the image data (for example, if it is a lossless raster image), it is possible that this data exceeds 16 MB, which, as you stated, is the maximum supported Realm size for binary properties.

When you call NSData.length, how big is your data saying?

0
source

Source: https://habr.com/ru/post/1258532/


All Articles