xcode - Setting Up an image in swift using NSuserdefaults -
i'm building swift game , need set images. code works string or integer:
for var = 0; < globalcurrentmembers.count; ++i { var membersdefaultname = nsuserdefaults.standarduserdefaults() membersdefaultname.setvalue(globalcurrentmembers[i].name, forkey: "globalcurrentmembersname\(i)") membersdefaultname.synchronize() }
but have error image
for var = 0; < globalcurrentmembers.count; ++i { var membersdefaultimage = nsuserdefaults.standarduserdefaults() membersdefaultimage.setvalue(globalcurrentmembers[i].image, forkey: "globalcurrentmembersimage\(i)") membersdefaultimage.synchronize() }
globalcurrentmembers
array of member
wich looks that:
class member { var image = uiimage () var name = string () var progression = int() var round = int() var level = int() var imageprogression = [uibutton]() func init(){ image = uiimage(named: "default.png")! name = "default" progression = 0 round = 0 level = 0 } }
so please can tell me how doing that.
uiimage not implement nscoding protocol, need convert uiimage nsdata first.
change code following:
var imagedata = uiimagepngrepresentation(globalcurrentmembers[i].image) var myencodedimagedata = nskeyedarchiver.archiveddatawithrootobject(imagedata) membersdefaultimage.setobject(myencodedimagedata, forkey: "globalcurrentmembersimage\(i)")
of course storing image nsuserdefaults not best practice, should store image in file directory.
Comments
Post a Comment