【问题标题】:Xamarin Forms MVVM (Prism) with Media.Plugin - How to get a taken picture from device storageXamarin Forms MVVM (Prism) with Media.Plugin - 如何从设备存储中获取照片
【发布时间】:2018-05-06 14:39:48
【问题描述】:

我正在使用 Xamarin Forms PCL (MVVM Prism) 和 MediaPlugin (https://github.com/jamesmontemagno/MediaPlugin)。

我有一个 viewModel 有一个名为 ImagePath 的公共属性

private string imagePath;
public string ImagePath
{
   get { return imagePath; }
   set { SetProperty(ref imagePath, value, "ImagePath"); }
}

在我的 XAML 中我有:

<Image Source="{Binding ImagePath}" HeightRequest="100" WidthRequest="100"/>

当我将图像 url(来自网络)设置为 ImagePath 属性时,它可以正常工作。 (我正在设置这样的字符串:“http://www.myaddress.com/myimage.jpg”)

但是,当我使用以下代码(MediaPlugin)拍照时:

var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "Sample",
                Name = "test.jpg"
            });

ImagePath = file.Path;

// also tried: ImagePath = ImageSource.FromFile(file.Path).toString();

var file.Path 接收到这样的值:

"/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}"

有了这个 file.Path 值然后我设置 ImagePath 属性: ImagePath = file.Path;

但是拍摄的图像没有出现在屏幕上。

我也尝试过使用 MediaPlugin 文档中建议的 Image.Source 属性,但是我没有达到我想要的结果。

我也尝试了以下方法:

公共财产进入虚拟机:

private Image image = new Image();
        public Image Image
        {
            get { return image; }
            set { SetProperty(ref image, value, "Image"); }
        }

Xaml:

<Image Source="{Binding Image.Source}" HeightRequest="100" WidthRequest="100"/>

设置文件值后(如前所示):

image.Source = ImageSource.FromStream(() =>
                    {
                        var stream = file.GetStream();
                        file.Dispose();
                        return stream;
                    });

有什么方法可以让它工作吗? 谢谢!!

【问题讨论】:

  • 您好,我遇到了同样的问题,我只是想知道您是否仍然可以使用已解决的答案访问此解决方案?下面的答案对我没有好处,我仍然看不到图片。所以也许我做错了。
  • 这里有同样的问题。从视图模型中可观察集合中的项目属性设置图像源,但没有图像显示。我正在尝试显示应用程序拍摄的图像(不是嵌入式资源)。有什么建议吗?

标签: mvvm xamarin.forms prism


【解决方案1】:

媒体插件返回一个媒体文件。

我将它存储在一个用于存储和显示的类中

public class IncidentImage
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public int IncidentDetailsId { get; set; }

    public byte[] ImageBytes { get; set; }

    [Ignore]
    public ImageSource ImageSource
    {
        get
        {
            ImageSource retval = null;
            if (ImageBytes != null)
            {
                retval = ImageSource.FromStream(() => new MemoryStream(ImageBytes));
            }
            return retval;
        }
    }
}

这样

    var incidentImage = new IncidentImage
    {
        ImageBytes = image.ToByteArray()
    };

图像是插件返回的媒体文件。

那么我的 XAML 看起来像这样

<Image Source="{Binding IncidentImage.ImageSource}" Aspect="AspectFit"  />

您也可以通过将 ImageSource 代码移至转换器来实现。

【讨论】:

  • 太好了,成功了!!你有任何关于将这个 ImageSource 类型发布到 sql 数据库的 tib 吗?还是保存文件并上传更好?
  • 我将 IncidentImage 类型保存到本地 Sqlite 数据库并通过 REST API 服务上传
【解决方案2】:

嗨,史蒂夫,我照你说的做,但是,我没有显示我的照片 第一个问题是 image.ToByteArray()

我用那个代码解决它

var memoryStream = new MemoryStream();
file.GetStream().CopyTo(memoryStream);
file.Dispose();

var incidentImage = new IncidentImage
 {
   ImageBytes = memoryStream.ToArray()
 };`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2019-08-14
    • 2017-01-04
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多