0

I'm using this function to draw image on another images i'm using for in loop to iterate over and over so please help me to solve this issue every time i render it takes 50+mb each time

func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage? {
    return autoreleasepool { () -> UIImage? in
        UIGraphicsBeginImageContext(size)
        draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image.draw(in: rect)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
}

this is methood i'm calling when i get the image from server and trying to add image on the top of another image

private func modifyImagse() {
    var parentSignatureCount = 0
    var childSignatureCount = 0
    guard let form = form else { return }
    guard form.pages.count > 0 else { return }
    var pages = downloadedImages
    for input in form.inputs ?? [] {
        for fieldId in input.fieldIds {
            for field in form.fields {
                if fieldId == field.id {
                    if input.inputType == .signature && input.label == "Signature"{

                        parentSignatureCount = parentSignatureCount + 1
                            let page = form.pages[field.pageNumber]
                            let x = field.x / 100.0 * page.width
                            let y = field.y / 100.0 * page.height
                            let signIndicator = UIImage(named: "sign_here_no_number.png")
                            pages[field.pageNumber] = pages[field.pageNumber].image(byDrawingImage: signIndicator!,
                                                                                    inRect: CGRect(x: CGFloat(x), y: CGFloat(y), width: 500, height: 150)) ?? pages[field.pageNumber]
                            pages[field.pageNumber] =
                                pages[field.pageNumber]
                                    .textToImage(drawText: "\(parentSignatureCount)", atPoint: CGPoint(x: x + 60, y: y + 10)                                                                , textFontHeight: 70)
                            pages[field.pageNumber] = pages[field.pageNumber]

                    }else if input.inputType == .signature && input.label == "Child Signature" {
                        childSignatureCount = childSignatureCount + 1
                        let page = form.pages[field.pageNumber]
                        let x = field.x / 100.0 * page.width
                        let y = field.y / 100.0 * page.height
                        let signIndicator = UIImage(named: "sign_here_child.png")
                        pages[field.pageNumber] = pages[field.pageNumber].image(byDrawingImage: signIndicator!,
                                                                                inRect: CGRect(x: CGFloat(x), y: CGFloat(y), width: 500, height: 150)) ?? pages[field.pageNumber]
                        pages[field.pageNumber] =
                            pages[field.pageNumber]
                                .textToImage(drawText: "\(childSignatureCount)", atPoint: CGPoint(x: x + 60, y: y + 10)                                                                , textFontHeight: 70)
                        pages[field.pageNumber] = pages[field.pageNumber]

                    }

                }
            }
        }
    }
    downloadedImages = pages
}
ketaki Damale
  • 634
  • 7
  • 25
  • You have 3 nested for-loops with some ```.textToImage``` methods inside. Of course it will eat up tons of memory. Not sure what you're trying to achieve here, but if this is the only solution for you, it means you'll have to be prepared it will slow down the devices, or even crash on less performant devices. – Starsky Dec 05 '19 at 10:21
  • i have also trying outside the loop but the same issue occur its eatup 50+mb for this – Talal Tariq Dec 05 '19 at 11:01
  • my question is why it have not release the memory after is rendered , it should release – Talal Tariq Dec 05 '19 at 11:04
  • my question is why it have not release the memory after is rendered , it should release ,when i'm trying to draw image using loop or without loop it take same memory when next time i got to relevant vc and again generate its take more memory means issue is there the previous memory not released properly – Talal Tariq Dec 05 '19 at 11:28
  • 1
    Firstly, even after the image is rendered, that does not mean it will release memory. The new image will need to be held in memory somewhere while it's being used. Secondly, if you're saying that the next time you go to the "relevant vc" and generate it again that it's taking more memory, it probably means that you're not destroying your view controller correctly when you navigate away from it, or are storing the image somewhere/somehow – Mr.P Dec 05 '19 at 12:27
  • @Mr.P is right. We don't know what happens when you ```next time go to relevant vc```. Show that step. – Starsky Dec 05 '19 at 12:59
  • @Starsky and Mr.p thank you i get what you said and thank your time i just find the issue i'm using sdwebimage cache for download images and when i'm trying to rendered images its also cache "UIGraphicsBeginImageContext(self.size)" and does't release memory so i'm trying after disabling cache its works fine now – Talal Tariq Dec 05 '19 at 14:24
  • thank you guys for your help and i just want one more question @Mr.P how can i destroy the vc which is currently in the navigation stack means when i go to root vc after popviewController deinit does't call how can solve this issue but when i'm dismiss the same vc deinit call perfectly – Talal Tariq Dec 05 '19 at 14:25
  • I can't figure that out without seeing your code unfortunately – Mr.P Dec 05 '19 at 14:29
  • Sometimes ```deinit``` isn't being called if there are some strong references to that vc. Read [here](https://stackoverflow.com/questions/26971415/deinit-never-called). – Starsky Dec 05 '19 at 14:34

0 Answers0