i able save uiimage array created on apple watch watchos , play series of images animation group background. can make image array , play cannot figure out how store/save these images can retrieve/load them next time run app don't have build them every time app runs.
here example of how building images core graphics (swift 3):
import watchkit import foundation class interfacecontroller: wkinterfacecontroller { @iboutlet var colourgroup: wkinterfacegroup! override func awake(withcontext context: anyobject?) { super.awake(withcontext: context) } override func willactivate() { var imagearray: [uiimage] = [] imagenumber in 1...250 { let newimage: uiimage = drawimage(fade: cgfloat(imagenumber)/250.0) imagearray.append(newimage) } let animatedimages = uiimage.animatedimage(with:imagearray, duration: 10) colourgroup.setbackgroundimage(animatedimages) let imagerange: nsrange = nsrange(location: 0, length: 200) colourgroup.startanimatingwithimages(in: imagerange, duration: 10, repeatcount: 0) super.willactivate() } func drawimage(fade: cgfloat) -> uiimage { let boxcolour: uicolor = uicolor(red: 1.0, green: 1.0, blue: 1.0, alpha: fade) let opaque: bool = false let scale: cgfloat = 0.0 let bounds: cgrect = wkinterfacedevice.current().screenbounds let imagesize: cgsize = cgsize(width: bounds.width, height: 20.0) uigraphicsbeginimagecontextwithoptions(imagesize, opaque, scale) let radius: cgfloat = imagesize.height/2.0 let rect: cgrect = cgrect(x: 0.0, y: 0.0, width: imagesize.width, height: imagesize.height) let selectorbox: uibezierpath = uibezierpath(roundedrect: rect, cornerradius: radius) let boxlinewidth: double = 0.0 selectorbox.linewidth = cgfloat(boxlinewidth) boxcolour.setfill() selectorbox.fill() // return image let result: uiimage = uigraphicsgetimagefromcurrentimagecontext()! uigraphicsendimagecontext() return result } override func diddeactivate() { // method called when watch view controller no longer visible super.diddeactivate() } }
basically looking way save , load [uiimage] in manner can use uiimage.animatedimage(with:[uiimage], duration: timeinterval) array
is there way save image array can load next time run app rather rebuild images?
thanks
greg
nskeyedarchiver , nskeyedunarchiver did trick. here swift code xcode 8b4:
override func willactivate() { var imagearray: [uiimage] = [] let filename: string = "therings" let filemanager = filemanager.default let url = filemanager.urls(for: .documentdirectory, in: .userdomainmask).first! nsurl let theurl: url = url.appendingpathcomponent(filename)! if let rings = nskeyedunarchiver.unarchiveobject(withfile: theurl.path) as? [uiimage] { print("retrieving rings - found rings") imagearray = rings } else { print("retrieving rings - can't find rings, building new ones") imagenumber in 1...250 { let newimage: uiimage = drawimage(fade: cgfloat(imagenumber)/250.0) imagearray.append(newimage) } nskeyedarchiver.archiverootobject(imagearray, tofile: theurl.path) } let animatedimages = uiimage.animatedimage(with:imagearray, duration: 10) colourgroup.setbackgroundimage(animatedimages) let imagerange: nsrange = nsrange(location: 0, length: 200) colourgroup.startanimatingwithimages(in: imagerange, duration: 10, repeatcount: 0) super.willactivate() }
Comments
Post a Comment