0

I need to pass a image from one view controller to another I had user NSuserdefault to declare the image variable as global but it shows this error app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object

 func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})  
imageview.image = image
NSUserDefaults.standardUserDefaults().setObject(image, forKey:"image")
NSUserDefaults.standardUserDefaults().synchronize()
}
  • If you want to pass an image which is available locally, then pass the name of the image as String to another VC, there get the image by name. If you want to load an image from server pass the respective url to another VC, there are load image from imageview. If you want to pass image itself, then pass it as NSData to another VC. For passing a data between two VCs, refer here http://stackoverflow.com/questions/27420772/passing-data-between-two-uiviewcontrollers-using-swift-and-storyboard/27420861#27420861 – Suresh Kumar Durairaj Jan 09 '15 at 07:13
  • I want to pass the image itself to another VC can you please explain it clearly using NSData – Vaigunda anand M Jan 09 '15 at 07:17
  • It's a simple assignment, create a variable of type NSData in sevondVC, get the firstVC's image as NSData format assign it to the secondVC's NSData variable before going to the secondVC. Refer the url what I shared find the line "secondVC.passedString = """. – Suresh Kumar Durairaj Jan 09 '15 at 07:40

4 Answers4

0

What do you means? Pass a UIImage's variables to another viewController? The information you provide is too little.

Maybe you want to look this link Using Delegation to Communicate With Other View Controllers.

jkyin
  • 168
  • 1
  • 12
0

Simply create a property of NSString *imageName and setting it before pushing/presenting a viewcontroller ,and in your other viewcontroller you could read this property to create a UIImage.

nikhil84
  • 3,235
  • 4
  • 22
  • 43
0

You can't save image directly into the UserDefaults, You can compress image to png/jpeg data,and you can save the data into the userDefault. using method

NSData *UIImagePNGRepresentation(UIImage *image);

and

if imageData {
    NSUserDefaults.standardUserDefaults().setObject(imageData, forKey:"image")
    NSUserDefaults.standardUserDefaults().synchronize()
}

when you read the userDefaults, you can read data firstly, then convert data to image using method

UIImage-imageWithData: or swift style UIImage(data:)

generally, we don't do like above, we save image to file, and save the file's path into the userDefault

Suen
  • 1,110
  • 1
  • 7
  • 13
0

To store the image with NSUSerdefaults:

var testimage = UIImage(named: "testimage.png");
NSUserDefaults.standardUserDefaults().setObject(UIImagePNGRepresentation(testimage), forKey:"image")
}

To retrieve it in another class:

var imagedata: NSData = NSUserDefaults.standardUserDefaults().dataForKey("image")!
        imageview.image = UIImage (data: imagedata)
Sabrina Tuli
  • 160
  • 7