Cannot convert SfImageEditor to UIView

Hi everyone, i'm upgrade my code from Xamarin.iOS to MAUI .net8. In my old project, i using UIView with Syncfusion.ImageEditor.iOS and now have change to Syncfusion.Maui.ImageEditor and the new of it is i can't use ImageEditor in Add() function of UIViewController anymore, it throw an exception that 

Error CS1503 Argument 1: cannot convert from 'Syncfusion.Maui.ImageEditor.SfImageEditor' to 'UIKit.UIView' Project.iOS (net8.0-ios17.2). Can anyone help me to solve this problem? Below is the code that having problem:

        public SfImageEditor EditorView

        {

            get

            {

                if (_editorView == null)

                {

                    _editorView = new SfImageEditor();

                    _editorView.SetToolbarItemVisibility("save", false);

                    _editorView.ImageSaving += EditorView_ImageSaving;

                    //

                    Add(_editorView);

                }

                return _editorView;

            }

            set { _editorView = value; }

        }

Thank you.



5 Replies

VM Vidyalakshmi Mani Syncfusion Team April 1, 2024 01:41 PM UTC

Hi Nguyên ,


Thank you for reaching out to us. To resolve the issue you are facing, you can use native embedding in iOS. Please follow these steps:


  • Create an iOS Application project in Visual Studio.
  • Enable .NET MAUI support in your iOS project by setting the $(UseMaui) and $(MauiEnablePlatformUsings) build properties to true in the project file as shown in the code snippet below:

 

<PropertyGroup>

  <Nullable>enable</Nullable>

  <UseMaui>true</UseMaui>

  <ImplicitUsings>enable</ImplicitUsings>

</PropertyGroup>

 


  • Modify the FinishedLaunching method in the AppDelegate class to create the main view controller. Create an instance of `SfImageEditor`, convert it to a `UIView` using the `ToPlatform` method, and add it as a subview to a `UIViewController`. Please see the following code snippets:


 

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)

{

    // create a new window instance based on the screen size

    Window = new UIWindow(UIScreen.MainScreen.Bounds);

 

 

    MauiAppBuilder builder = MauiApp.CreateBuilder();

    builder.UseMauiEmbedding<Microsoft.Maui.Controls.Application>();

    builder.ConfigureSyncfusionCore();

 

    // Register the Window

    builder.Services.Add(new Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(UIWindow), Window));

    MauiApp mauiApp = builder.Build();

    _mauiContext = new MauiContext(mauiApp.Services);

 

    //Create an instance of SfImageEditor

    SfImageEditor imageEditor = new SfImageEditor();

    imageEditor.Source = "image.png";

 

    //Convert `SfImageEditor` to `UIView`

    UIView mauiView = imageEditor.ToPlatform(_mauiContext);

    mauiView.Frame = Window!.Frame;

 

    // create a UIViewController

    var vc = new UIViewController();

 

    // Add the UIView to the UIViewController

    vc.View!.AddSubview(mauiView);

 

    Window.RootViewController = vc;

 

    // make the window visible

    Window.MakeKeyAndVisible();

 

    return true;

}

 


By following these steps, you can integrate the MAUI SfImageEditor control into your MAUI project using native embedding in iOS. We have prepared a simple sample demonstrating this. Please see the attached sample for your reference.


For more information on native embedding, please refer the following link: Dotnet/maui/platform-integration/native-embedding


If you have any questions or encounter any issues, please let us know.


Regards,

Vidyalakshmi M.



Attachment: NativeEmbeddingImageEditor_c35e0ef9.zip


NT Nguyên Tru?ng replied to Vidyalakshmi Mani April 2, 2024 02:40 PM UTC

If the sfImageEditor i use in a library project and the MAUI project reference to it, how can i convert it SfImageEditor to UIView?

Thank you



VM Vidyalakshmi Mani Syncfusion Team April 3, 2024 01:32 PM UTC

Hi Nguyên ,


In the` FinishedLaunching` method within the AppDelegate class, you can create a MauiContext from mauiApp.Services to provide context for MAUI controls. Then, you can create an instance of SfImageEditor, convert it to a UIView using the ToPlatform method, and subsequently add it as a subview to a UIViewController. Here is a  code snippet for your reference:


public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)

{

    // create a new window instance based on the screen size

    Window = new UIWindow(UIScreen.MainScreen.Bounds);

    MauiAppBuilder builder = MauiApp.CreateBuilder();

builder.UseMauiEmbedding<Microsoft.Maui.Controls.Application>();

    builder.ConfigureSyncfusionCore();

    // Register the Window

    builder.Services.Add(new Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(UIWindow), Window));

    MauiApp mauiApp = builder.Build();

 

    _mauiContext = new MauiContext(mauiApp.Services);

    //Create an instance of SfImageEditor

    SfImageEditor imageEditor = new SfImageEditor();

    imageEditor.Source = "image.png";

    //Convert `SfImageEditor` to `UIView`

    UIView mauiView = imageEditor.ToPlatform(_mauiContext);

    mauiView.Frame = Window!.Frame;

    // create a UIViewController

    var vc = new UIViewController();

    // Add the UIView to the UIViewController

    vc.View!.AddSubview(mauiView);

    Window.RootViewController = vc;

    // make the window visible

    Window.MakeKeyAndVisible();

    return true;

}


Please review the attached sample for a more detailed implementation. If this doesn't resolve your issue, kindly modify the sample to replicate the problem in your scenario and send us the modified sample. If modifying the sample is not feasible, please provide us with the sample where you encountered the issue. This will greatly assist us in identifying the problem and providing a suitable solution.


Regards,

Vidyalakshmi M.



Attachment: NativeEmbeddingImageEditor_f497237a.zip


NT Nguyên Tru?ng replied to Vidyalakshmi Mani April 8, 2024 03:40 PM UTC

And can i ask how to convert an UIImage to Sgf



VM Vidyalakshmi Mani Syncfusion Team April 9, 2024 12:37 PM UTC

Hi Nguyên,


To better assist you, could you please provide more details on why you need to convert UIImage to SGF? Additionally, could you explain what you plan to do with the SGF in the image editor? This information will help us understand your needs more clearly and provide you with the most suitable guidance.


Regards,

Vidyalakshmi M.



Loader.
Up arrow icon