Save an image to a device's photo library

How to save an image to the device's photo library on iOS and Android.

  •         
    viewModel.saveToAlbum = function(imageSource, format, quality, callBack) {
        if (app.ios) {
            var res = false;
            if (!imageSource) {
                return res;
            }
            var result = true;
            var CompletionTarget = NSObject.extend({
                "thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:": function(
                    image, error, context) {
                    if (error) {
                        result = false;
                    }
                }
            }, {
                exposedMethods: {
                    "thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:": {
                        returns: interop.types.void,
                        params: [UIImage, NSError, interop.Pointer]
                    }
                }
            });
            var completionTarget = CompletionTarget.new();
            UIImageWriteToSavedPhotosAlbum(imageSource.ios, completionTarget,
                "thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:",
                null);
                if (callBack) callBack();
            return result;
        } else {
            function broadCast(imageFile) {
    
                var mediaScanIntent = new android.content.Intent(android.content.Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                var contentUri = android.net.Uri.fromFile(imageFile);
                mediaScanIntent.setData(contentUri);
                app.android.foregroundActivity.sendBroadcast(mediaScanIntent);
                if (callBack) callBack();
                return result;
            }
    
            var folderPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES).toString();
            var fileName = 'img_' + new Date().getTime() + '.jpg';
            var path = fsModule.path.join(folderPath, fileName);
            var exists = fsModule.File.exists(path);
            if (!exists) {
                var saved = imageSource.saveToFile(path, enums.ImageFormat.jpeg);
                if (saved) broadCast(new java.io.File(path));
            }        
        }
    };