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 {
source share