You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin使用ImageCropper裁剪后无法读取非项目路径图片

解决ImageCropper.Forms裁剪后图片无法加载的问题

你的问题核心是错误使用了ImageSource.FromResource方法——这个方法仅用于加载嵌入到项目程序集内的资源文件(比如添加到项目中并设置为「嵌入资源」的图片),而裁剪后的图片存储在设备文件系统的沙盒路径中,完全不适用这个加载方式。

正确修复方案

将ImageSource.FromResource(imageFile)替换为ImageSource.FromFile(imageFile),直接加载本地文件路径即可,修改后的代码如下:

async void OnchangePicture(System.Object sender, System.EventArgs e)
{
    new ImageCropper()
    {
        CropShape = ImageCropper.CropShapeType.Oval,
        Success = (imageFile) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                // 改用FromFile加载本地沙盒路径的图片
                ProfileImage.Source = ImageSource.FromFile(imageFile);
                // 等价写法:ProfileImage.Source = new FileImageSource { File = imageFile };
            });

            Console.WriteLine("Cropped File Path " + imageFile);
            Console.WriteLine("Photo Path " + result.FullPath);
        }
    }.Show(this, result.FullPath);
}

补充说明

  • iOS应用的Documents目录属于应用沙盒内部路径,应用本身拥有完全访问权限,无需额外申请文件访问权限。
  • 如果后续需要对图片做持久化管理或同步,可以考虑将裁剪后的文件复制到自定义目录,但当前场景下直接用FromFile就能解决加载问题。

内容的提问来源于stack exchange,提问作者hk77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 12:48:25