简体   繁体   English

如何将我的照片从 tmp 目录文件夹保存到 xamarin iOS 中的库/文档文件夹

[英]How Do I Save my photo from tmp directory folder to library/ document folder in xamarin iOS

I want to save my photo from the temp folder to the library or document folder in Xamarin IOS.我想将我的照片从临时文件夹保存到 Xamarin IOS 中的库或文档文件夹中。 I use iPhone Simulator for that and Xamarin iOS native for my development.我为此使用 iPhone 模拟器,并为我的开发使用 Xamarin iOS 本机。 Firstly, I get the URL path then I change to the real path.首先,我得到 URL 路径,然后我更改为真实路径。 So far, I got the real path:到目前为止,我得到了真正的路径:

NSUrl myFileUrl = new NSUrl(ImageValue);
            Console.WriteLine(myFileUrl.AbsoluteString);
            FromGalleryPath= myFileUrl.Path;

But the result of the path is但是路径的结果是

.../Application/xxx/tmp/5B4AFD16E192.jpeg

I don't want to store my image in the tmp --> Application/xxx/tmp/ because, every time when I restart my emulator then the picture gone?我不想将我的图像存储在 tmp --> Application/xxx/tmp/中,因为每次我重新启动模拟器时,图像就消失了? How can I save my image into the library/ document folder and how to retrieve back the path from the library/ document folder?如何将我的图像保存到库/文档文件夹中,以及如何从库/文档文件夹中检索路径?

I've searched on the internet but, still no idea.我在互联网上搜索过,但仍然不知道。 Can anyone help me?谁能帮我?

Thank in advance.预先感谢。

Using NSSearchPathForDirectoriesInDomains the same as native code can get the path.After searching, NSSearchPath can achieve that.使用NSSearchPathForDirectoriesInDomains和原生代码一样可以获取路径。搜索后, NSSearchPath可以实现。 Have a try with follow code to accesee Library/ path:尝试使用以下代码访问Library/路径:

Create a static class NSSearchPath :创建一个 static class NSSearchPath

public static class NSSearchPath
{
    public static string[] GetDirectories(NSSearchPathDirectory directory, NSSearchPathDomain domainMask, bool expandTilde = true)
    {
        return NSArray.StringArrayFromHandle(NSSearchPathForDirectoriesInDomains((nuint)(ulong)directory, (nuint)(ulong)domainMask, expandTilde));
    }

    [DllImport(Constants.FoundationLibrary)]
    static extern IntPtr NSSearchPathForDirectoriesInDomains(nuint directory, nuint domainMask, bool expandTilde);
}

Uesd as follow:用法如下:

string[] paths = NSSearchPath.GetDirectories(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.All);
Console.WriteLine(paths[0]);

Output Log: Output 日志:

2020-05-20 13:52:11.254843+0800 AppIOS2[4340:108912] /Users/xxxx/Library/Developer/CoreSimulator/Devices/4C2C6DD2-3D42-46EA-8078-D6E0B851AEDD/data/Containers/Data/Application/DB913987-0E55-4603-8993-ED7B3F83CF85/Library

Reference docs: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system#application-directories参考文档: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system#application-directories

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM