【问题标题】:ImageCropper.Forms is not working in ios platform (System.MissingMethodException)ImageCropper.Forms 在 ios 平台上不工作 (System.MissingMethodException)
【发布时间】:2021-02-02 02:11:36
【问题描述】:

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

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

找到了同样的问题here,但没有解决我的问题。

我的代码

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 Offline Packages, nuget.org ImageCropDemo C:\Users\user\Downloads\ImageCropDemo\ImageCropDemo\ImageCropDemo\ImageCropDemo.csproj 1=
错误包还原失败。回滚“ImageCropDemo”的包更改。

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

我已经上传了一个示例here 供参考。

【问题讨论】:

    标签: ios xamarin.forms


    【解决方案1】:

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

    如果你确实想实现它,你可以使用插件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 项目站点中的示例。

    【讨论】:

    • 在我的情况下,相机或画廊没有打开。当我选择该选项时,我得到了那个例外。这是一个不错的包,有什么方法可以在 ios 中解决这个问题? Xamarin.Plugin.ImageEdit 对用户不友好。
    • 你可以将问题发布到 github issue ,我已经添加了每个修复包但都没有工作。
    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 2011-02-27
    • 2021-05-03
    • 2022-12-05
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多