i want able save array, cardimages
, contains uiimages via swiftyuserdefaults.
desired behavior
here exact desired behavior:
save array of uiimages nsuserdefaults via swiftyuserdefault library
retrieve images later
code stripped down little code
var newphotokey = defaultskey<nsarray>("image")//setting swiftyuserdefaults persisted array cardimages = [(uiimage(named: "myimagename.jpg")!)] //this array contains default value, , fill more defaults[thekeyforstoringthisarray] = cardimages //this persisted array in array full of images should stored. error happens var arraytoretreivewith = defaults[thekeyforstoringthisarray] as! [uiimage] //to retreive
error
i following error:
attempt set non-property-list object ( ", {300, 300}" ) nsuserdefaults/cfpreferences value key image *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'attempt insert non-property list object ( ", {300, 300}" ) key image'
thanks!
the error message clear actually. uiimage not propertylist need change row data first. i'll put example below fyi saving big data images using nsuserdefaults not recommended. i'd use nsfilemanager , put in user documents directory. anyway
var newphotokey = defaultskey<nsarray>("image") cardimages = [(uiimage(named: "myimagename.jpg")!)] var cardimagesrowdataarray: nsdata = [] image in cardimages { let imagedata = uiimagejpegrepresentation(image, 1.0) cardimagesrowdataarray.append(imagedata) } defaults[thekeyforstoringthisarray] = cardimagesrowdataarray var arraytoretreivewith = defaults[thekeyforstoringthisarray] as! [nsdata] // here can use uiimage(data: data)
if don't insist on using swiftyuserdefaults, can save in user documents directory, here how
func saveimage(image: uiimage){ if let imagedata = uiimagejpegrepresentation(image, 1.0) { let manager = nsfilemanager() if let docurl = manager.urlsfordirectory(.documentdirectory, indomains: .userdomainmask).first{ let uniquename = nsdate.timeintervalsincereferencedate() let url = docurl.urlbyappendingpathcomponent("\(uniquename).jpg") imagedata.writetourl(url, atomically: true) } } }
Comments
Post a Comment