ios - Loading multiple saved images in app slows it down -
i developing app user can save 13 screenshots , display them on single view thumbnails or full screen image.
let filename:string = self.stickerused + ".png" var arraypaths = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)[0] nsstring var pngfilename = arraypaths.stringbyappendingpathcomponent(filename) uiimagepngrepresentation(screenshot).writetofile(pngfilename, atomically:true) nsuserdefaults.standarduserdefaults().setobject(filename, forkey: self.stickerused) nsuserdefaults.standarduserdefaults().synchronize()
this above how save image , following how retrieve images. code first screenshot:
var defaultname:string = "sticker1.png" let path = nssearchpathfordirectoriesindomains( .documentdirectory, .userdomainmask, true)[0] nsstring let filename = nsuserdefaults.standarduserdefaults() .stringforkey("sticker1") ?? defaultname let imagepath = path.stringbyappendingpathcomponent(filename) let image = uiimage(contentsoffile: imagepath )
the problem displaying them in single view gets slow number of screenshot increases. app crashes after showing "received memory warning". new swift , app development. right way display these images in thumbnail without slowing down or crashing , saving image in full resolution.
the problem following code have images in full resolution:
let image = uiimage(contentsoffile: imagepath )
it better scale down , make thumbnail after loaded memory:
// line still same let image = uiimage(contentsoffile: imagepath ) // scale image down: let newsize = ... // defined new size here // should idea match // size of uiimageview uigraphicsbeginimagecontext(newsize) image.drawinrect(cgrect(origin: cgpointzero, size: newsize)) image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext()
see more: link.
Comments
Post a Comment