Image Editor

Imports

To integrate the Truvideo SDK, include the following platform-specific imports:

#if IOS
using Foundation;
using UIKit;
using TruvideoImageiOS;
#endif

#if ANDROID
using Application = Android.App.Application;
using TruVideoImageAndroidBinding;
using TruVideoImageLib = TruVideoImageAndroid.DotnetTruvideoImage;
#endif

Async Callback Wrapper

For handling asynchronous callbacks in Android, use the following method:

#if ANDROID
 private Task<T> ExecuteImageCallbackAsync<T>(Action<ImageCallback> action) {
        var tcs = new TaskCompletionSource<T>();

        action.Invoke(new ImageCallback(
            success => MainThread.BeginInvokeOnMainThread(() =>
                tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
            failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
        ));

        return tcs.Task;
    }
#endif

This function executes image-related actions asynchronously and ensures results are processed on the main thread.

Editing an Image

Android implementation:

#if ANDROID
var inputPath = filePath.ToString();
var outputPath = await ExecuteImageCallbackAsync<string>(cb => TruVideoImageLib.GetResultPath(Application.Context,  inputPath, cb));
var imageResult = await ExecuteImageCallbackAsync<string>(cb => TruVideoImageLib.EditImage(Application.Context, inputPath, outputPath, cb));
LogTextEditor.Text = $"Edited Image : {imageResult}";        
#endif

iOS implementation:

#if IOS
String filePath = inputPath;
Uri fileUri = new Uri(filePath);
String directory = Path.GetDirectoryName(filePath) ?? throw new InvalidOperationException();
String fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
String outputFileName = $"{fileNameWithoutExtension}_EditedImage.jpg";
String outputPath = Path.Combine(directory, outputFileName);
Uri outputUri = new Uri(outputPath);
var viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;

TruvideoImageSdk.Shared.EditImage(inputUrl: fileUri, outputUrl: outputUri, viewController: viewController,
    (resultUrl, error) =>
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            
            if (error != null)
            {
                // Handle error
                Console.WriteLine($"Error to edit Image: {error.LocalizedDescription}");
                LogTextEditor.Text = $"Error to edit Image: {error.LocalizedDescription}";
                return;
            }

            // Handle success
            Console.WriteLine($"Image Edited at: {resultUrl}");
            LogTextEditor.Text = $"Image Edited at: {resultUrl}";
            string resultFilePath = resultUrl.Path; // Use Path instead of LocalPath

            // Move or rename the result file to the desired output path
            if (File.Exists(resultFilePath))
            {
                File.Move(resultFilePath, outputPath, true);
                Console.WriteLine($"Edited Image saved at: {outputPath}");
                LogTextEditor.Text = $"Edited Image saved at: {outputPath}";
            }
        });
    });
#endif

Last updated

Was this helpful?