swift - iOS: Memory leak when try to scale the UIImage -


memory leak happens when run code below. haven't used or hold newimage object.

for image in imagearray {     autoreleasepool {         let newimage = image.scalingwith(targetsize: cgsize(width: 500, height: 500))     } } 

and here scale method in uiimage extension:

func scalewith(targetsize: cgsize, backgroundcolor: uicolor = uicolor     .white) -> uiimage? {     let imagesize = size     var scalefactor: cgfloat = 0.0     let scaledwidth = targetsize.width     let scaledheight = targetsize.height     var thumbnailpoint = cgpoint.zero      if imagesize.equalto(targetsize) { return self }      let widthfactor = targetsize.width / imagesize.width     let heightfactor = targetsize.height / imagesize.height      if widthfactor > heightfactor {         scalefactor = heightfactor         thumbnailpoint.x = (targetsize.width - scalefactor * imagesize.width) * 0.5;     } else {         scalefactor = widthfactor         thumbnailpoint.y = (targetsize.height - scalefactor * imagesize.height) * 0.5;     }      uigraphicsbeginimagecontext(cgsize(width: scaledwidth, height: scaledheight))      let context = uigraphicsgetcurrentcontext()     let bounds = cgrect(x: 0, y: 0, width: scaledwidth, height: scaledheight)     context!.setfillcolor(backgroundcolor.cgcolor)     context!.fill(bounds)      var thumbnailrect = cgrect.zero     thumbnailrect.size.width = imagesize.width * scalefactor;     thumbnailrect.size.height = imagesize.height * scalefactor;     thumbnailrect.origin = thumbnailpoint      draw(in: thumbnailrect)     let resultimage = uigraphicsgetimagefromcurrentimagecontext()      uigraphicsendimagecontext();      return resultimage } 

could give me advice?

generally easy run code in instruments memory leaks enabled. also, need run on device running on simulator produce wrong result. there bug in xcode not releasing objects when in debug mode , console used.

so again: run in instruments on device. code looks good, there should not memory leak. autoreleasepool needed there considering code inside loop?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -