PeterStaev / nativescript-photo-editor

🎨 Easily edit an image in your NativeScript app (crop, draw, etc)
Apache License 2.0
47 stars 15 forks source link

Memory leak in IOS #23

Closed plhearn closed 4 years ago

plhearn commented 5 years ago

I started using this plugin for an app to allows users drawing on photos. They started reporting crashes with the app and have traced it to a memory leak in the plugin. After testing with the memory profiler in the xcode debugger I've found the following:

  1. Creating a new photo editor instance works fine and does not cause any memory issues.

  2. The editPhoto function will allocate a certain amount of memory that will never go away. Its not much and depends on the size of the photo. However, even if you bring up the editor and cancel it without saving the memory will continue to go up. This happens even if there is no code inside photoEdit's .then() function.

  3. If you save an image onto the device using the editor the image will be kept in memory and will stay there until the app is closed and restarted. After enough edits the app will crash from memory overload.

Here is my code: ` takePicture_PhotoEdit() {

//var options = { width: 300, height: 300, keepAspectRatio: false, saveToGallery: false };
camera.takePicture().
    then((imageAsset) => {

        let photoEditor = new PhotoEditor();

        let newPath = this.folder.path + "/cameraPic_" + (new Date()).getTime();
        let newPathThumbnail = newPath + "_thumbnail.png"
        newPath = newPath + ".png";
        let img: ImageSource = new ImageSource();            
        img.fromAsset(imageAsset).then(imageSource => {
        imageSource.saveToFile(newPath, "png")

        photoEditor.editPhoto({
            imageSource: imageSource, // originalImage.imageSource,
            hiddenControls: [
                // PhotoEditorControl.Save,
                // PhotoEditorControl.Clear,
                // PhotoEditorControl.Draw,
                // PhotoEditorControl.Text,
            ],
        }).then((newImage: ImageSource) => {

            var test = CreateBitMap(newImage.width,newImage.height);
            test.insert(newImage);
            var resizedImage = test.resizeMax(100);

            var thumbnailImage = resizedImage.toImageSource();
            test.dispose();
            resizedImage.dispose();
            thumbnailImage.saveToFile(newPathThumbnail,"png");
            newImage.saveToFile(newPath, "png")

            let photoJson = {path: "file://" + newPath,thumb: "file://" + newPathThumbnail}

            if (this.item.photos){
              this.item.photos.push(photoJson);
            }
            else{
              this.item.photos = [photoJson]
            }

            this.checklistService.save();
            this.itemUpdated.emit()

        }).catch((e) => {
            console.log(e);
        });
      })

    }).catch((err) => {
        console.log("Error -> " + err.message);
    });

}

`

PeterStaev commented 5 years ago

Hey @plhearn , thanks for reporting this and taking the time to trace it! I have couple of hunches what might be causing this, but sadly I'm not sure when I will have the time to try some fixes.

amarseelam commented 4 years ago

There seems to be new commits on https://github.com/M-Hamed/photo-editor to address memory leaks. I am not sure if this plugin can simply use the latest version iOSPhotoEditor

PeterStaev commented 4 years ago

This should be resolved in the new version of the plugin available in the ProPlugins NPM registry.

All future work of this plugin will be available exclusively as part of ProPlugins initiative. So if you want to get the latest updates/fixes/features make sure you subscribe to the service. You get all my plugins plus many of the most used and high quality plugins for a small monthly subscription fee.