首页 > 解决方案 > ImageCropper.Forms 在 ios 平台上不工作 (System.MissingMethodException)

问题描述

我正在使用ImageCropper.Forms包来裁剪图像。它在 android 部分工作正常,但是当我在 ios 上尝试时,我得到以下异常:

异常:>System.MissingMethodException:找不到方法:System.Threading.Tasks.Task`1<Plugin.Media.Abstractions.MediaFile> Plugin.Media.Abstractions.IMedia.TakePhotoAsync(Plugin.Media.Abstractions.StoreCameraMediaOptions) 在系统。 Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] 在 /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime /compilerservices/AsyncMethodBuilder.cs:84 在 Stormlion.ImageCropper.ImageCropper.Show (Xamarin.Forms.Page 页面, System.String imageFile) [0x00033] in <548dc893a11b47fe908c9c3d7f4a39ba>:0 在 ImageCropDemo.MainPage.OnClickedRectangle (System.Object sender, System.EventArgs e) [0x00002] 在 /Users/companyname/Downloads/ImageCropDemo/ImageCropDemo/ImageCropDemo/MainPage。xml.cs:29

在这里找到了同样的问题,但它并没有解决我的问题。

我的代码

try
{
    new ImageCropper()
    {
        Success = (imageFile) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                image.Source = ImageSource.FromFile(imageFile);
            });
        }
    }.Show(this);
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
}

我试图安装ImageCropper.Forms.Fix.v3来解决这个问题。当我尝试安装时Fix.v3出现以下错误:

严重性代码描述项目文件行抑制状态错误 NU1101 无法找到包 Plugin.Persmissions。源中不存在具有此 ID 的包:Microsoft Visual Studio 脱机包,nuget.org ImageCropDemo C:\Users\user\Downloads\ImageCropDemo\ImageCropDemo\ImageCropDemo\ImageCropDemo.csproj 1=
错误包恢复失败。回滚“ImageCropDemo”的包更改。

安装成功,ImageCropper.Forms.Fix.v5但异常没有变化。ImageCropper.Forms.Fix.v6并且ImageCropper.Forms.Fix.v7包也可用,哪个包将在ios中解决这个问题?

我在这里上传了一个示例供参考。

标签: iosxamarin.forms

解决方案


这个问题似乎是由包本身引起的。这是一个类似的问题

如果您确实想实现它,您可以使用插件Xamarin.Plugin.ImageEdit来编辑您的图像。

将其安装到您的表单项目和每个平台项目中。

public byte[] GetImageStreamAsBytes(Stream input)
    {
        var buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

    async void OpenCamera(object sender, EventArgs args)
    {
        try
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("Alert", "No camera available.", "Ok");
                return;
            }

            _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "Sample",
                Name = "test.jpg",
                AllowCropping = true,
                PhotoSize = PhotoSize.Medium
            });

            if (_mediaFile == null)
                return;
            

            using (var newImage = await CrossImageEdit.Current.CreateImageAsync(GetImageStreamAsBytes(_mediaFile.GetStream())))
            {
                var croped = await Task.Run(() =>
                        newImage.Crop(0, 0, 250, 300)
                             .Rotate(180)
                             .Resize(100, 0)
                             .ToPng()
                );


                image.Source = ImageSource.FromStream(() => new MemoryStream(croped));
            }


        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("CameraException:>" + ex);
        }
    }

该插件只能裁剪具有固定值的图像。如果要在运行时设置区域,请查看 github 项目站点中的示例。


推荐阅读