【问题标题】:Download Photo not saved on local folder Xamarin Forms iOS下载照片未保存在本地文件夹 Xamarin Forms iOS
【发布时间】:2019-01-22 18:54:45
【问题描述】:

保存的图像未在 galary iOS 上显示(在 Android 上运行良好,但在 iOS 上运行良好) 这是我将图像保存到设备的代码

public void SavePicture(string name, byte[] data, string location = "Downloads")
        {
            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            documentsPath = Path.Combine(documentsPath, location);
            Directory.CreateDirectory(documentsPath);

            string filePath = Path.Combine(documentsPath, name);
            File.WriteAllBytes(filePath, data); // writes to local storage
}

还允许 info.plist 上的照片权限 隐私 - 照片库使用说明

执行此代码后,我在 iOS 设备上找不到保存的照片 如何解决?

【问题讨论】:

  • 将图像保存到 LocalStorage 与将其保存到照片库不同。你想做哪一个?
  • 其实我想把所有文件下载到我指定的本地文件夹中。
  • 您拥有的代码应该这样做。有问题吗?您是否收到错误或异常?
  • 我没有得到任何异常,这段代码执行了但是当我搜索下载文件时我找不到那个文件。

标签: xamarin xamarin.forms xamarin.ios


【解决方案1】:

在 iOS 中,要将图像显示到图库中,您必须编写额外的代码,因为仅使用 File.WriteAllBytes 不会调用本机 UIImageWriteToSavedPhotosAlbum API 来将其显示在相册中。

为此,您必须按如下方式调用此 API:

做一个依赖服务:

  public interface ISavePhotosToAlbum
 {
    void SavePhotosWithFilePath(String Path);
 }

现在添加一个原生类并执行以下操作:

 [assembly: Dependency(typeof(SaveToAlbum))]
 namespace SavePhotosDemo.iOS
{
      public class SaveToAlbum : ISavePhotosToAlbum
   {
       public void SavePhotosWithFilePath(String Path)
     {
            using (var url = new NSUrl (Path))
            using (var data = NSData.FromUrl (url))
            var image = UIImage.LoadFromData (data);          
            image.SaveToPhotosAlbum((img, error) =>
            {
                   if (error == null)
                   {//Success }                  
                   else
                   {//Failure}
            });
      }
   }
 }

然后按如下方式使用这个依赖服务:

DependencyService.Get<ISavePhotosToAlbum>().SavePhotosWithStream(imageUrl);

另外,请通过在 OnPlatform if 语句中添加此依赖项服务调用,仅针对 ios 添加此依赖项服务调用。

更新

使用以下代码在照片库中添加自定义相册:

       void AddAssetToAlbum(UIImage image, PHAssetCollection album, string imageName)
        {
            try
            {
                PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
                {
                    // Create asset request
                    var creationRequest = PHAssetCreationRequest.CreationRequestForAsset();
                    var options = new PHAssetResourceCreationOptions
                    {
                        OriginalFilename = imageName
                    };
                    creationRequest.AddResource(PHAssetResourceType.Photo, image.AsJPEG(), options);
                    // Change asset request (change album by adding photo to it)
                    var addAssetRequest = PHAssetCollectionChangeRequest.ChangeRequest(album);
                        addAssetRequest.AddAssets(new PHObject[] { creationRequest.PlaceholderForCreatedAsset });
                }, (success, error) =>
             {
                    if (!success)
                        Console.WriteLine("Error adding asset to album");
                });
            }
            catch (Exception ex)
            {
                string h = ex.Message;
            }
        }

请注意您使用PHPhotoLibrary.RequestAuthorization 方法请求授权,因为我们需要请求访问权限才能使用照片库。另外,自 iOS 10 起及以上版本我们还需要在目标 .plist 文件中为“Privacy - Photo Library Usage Description”添加访问条目:

<key>NSPhotoLibraryUsageDescription</key>
<string>Access to photos is needed to provide app features</string>

更新

最后,添加以下内容开始在文件应用程序中显示文档:

 <key>UIFileSharingEnabled</key> <true/> <key>LSSupportsOpeningDocumentsInPlace</key> <true/> 

【讨论】:

  • 我已经使用了这个 NSData,它工作正常,但你能告诉我如何在 Xamarin iOS 的文件夹中保存文档吗?
  • 您要将此保存到哪个特定文件夹?
  • 我想创建一个像“MyDownloads”这样的文件夹并将这个文件保存在这里。那么该怎么做呢?
  • 在我的应用程序中,我正在显示文档列表,当用户单击任何项​​目时,将项目转换为字节数组并将其保存到本地文件夹。我的代码已执行,但我保存的文档不是在我的本地文件夹中找到。
  • 那是因为你只保存在你的文档目录中!
猜你喜欢
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
相关资源
最近更新 更多