Xamarin Forms Labs 相机 - 永久保存图像并调用它们 [英] Xamarin Forms Labs Camera - Permanently Saving Images and Calling Them

查看:21
本文介绍了Xamarin Forms Labs 相机 - 永久保存图像并调用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让相机功能正常工作,它也像我要求的那样在页面上显示图像.但是有没有办法将图像永久保存在手机或其他地方然后调用它?

I got the camera function to work and it displays the image on the page like i asked it too. But is there a way to permanently save the image on your phone or somewhere else and then call it?

非常感谢

推荐答案

这是一些对我有用的代码.

Here's some code that works for me.

IFileAccess 是我对 System.IO.File 函数(例如文件打开、写入、检查是否存在)的包装.如果您要创建自己的文件服务,请查找 Xamarin.Forms.Labs.Resolver 以及如何使用它;如果您使用共享表单项目类型,您可以直接从表单项目访问 System.IO.File.假设这很清楚,以下

IFileAccess is my wrapper around System.IO.File functions such as file open, write, check if exsists. If you're making your own file service look up Xamarin.Forms.Labs.Resolver and how to use it; if you're using shared Forms project type you can access System.IO.File directly from the Forms project. Assuming that's clear, the following

var fileAccess = Resolver.Resolve<IFileAccess> (); 
mediaPicker.SelectPhotoAsync (new CameraMediaStorageOptions{ MaxPixelDimension = 1024 })
.ContinueWith(t=>{
  if (!t.IsFaulted && !t.IsCanceled) { 
    var mediaFile = t.Result;  

    var fileAccess = Resolver.Resolve<IFileAccess> ();
    string imageName = "IMG_" + DateTime.Now.ToString ("yy-MM-dd_HH-mm-ss") + ".jpg";

 // save the media stream to a file 
    fileAccess.WriteStream (imageName, mediaFile.Source);

 // use the stored file for ImageSource
    ImageSource imgSource = ImageSource.FromFile (fileAccess.FullPath (imageName)); 

    imgInXAML.Source = imgSource;
  }
});

<小时>

有关 IFileAccess 的更多详细信息.


Further detail on IFileAccess.

在你的表单项目中创建一个这样的界面:

In your Forms project create an interface like this:

public interface IFileAccess
{
    bool Exists (string filename);
    string FullPath(string filename); 
    void WriteStream (string filename, Stream streamIn);
}

在您的 iOS 或 Android 或共享项目中添加一个实现 IFileAccess 的类 FileAccess:

In your iOS or Android or Shared project add a class FileAccess that implements IFileAccess:

public class FileAccess : IFileAccess
{ 
    public bool Exists (string filename)
    {
        var filePath = GetFilePath (filename);

        if (File.Exists (filePath)) {
            FileInfo finf = new FileInfo (filePath);
            return finf.Length > 0;
        } else
            return false;
    }

    public string FullPath (string filename)
    {
        var filePath = GetFilePath (filename);
        return filePath;
    }

    static string GetFilePath (string filename)
    {
        var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
        var filePath = Path.Combine (documentsPath, filename);
        return filePath;
    }

    public void WriteStream (string filename, Stream streamIn)
    {
        var filePath = GetFilePath (filename);
        using (var fs = File.Create (filePath)) {
            streamIn.CopyTo (fs); 
        }
    }
}

如果您已经在使用 Xamarin.Forms.Labs.Resolver,则仅添加用于注册服务的行,否则在您的 iOS 或 Android 项目中找到对 Forms.Init() 的调用和在添加之前

If you're already using Xamarin.Forms.Labs.Resolver then add only the line to register the service, otherwise in your iOS or Android project find a call to Forms.Init() and right before it add

var resolverContainer = new SimpleContainer ();
resolverContainer.Register<IFileAccess> (t => new FileAccess ()); // maybe just this line
Resolver.SetResolver (resolverContainer.GetResolver ());

这篇关于Xamarin Forms Labs 相机 - 永久保存图像并调用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆