使用 Xamarin Forms 在 iPhone 或 iPad 的相册中保存照片 [英] Save Photos in Photo albums of iPhone or iPad using Xamarin Forms

查看:57
本文介绍了使用 Xamarin Forms 在 iPhone 或 iPad 的相册中保存照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在做一个项目,我需要从 url 下载文件.现在我正在下载文件并将其保存在个人文件夹中,并保存在应用程序目录中.我正在通过文件路径获取该文件并显示在网络视图中.但如果文件是图像或视频,我无法在 ipad 的相册或图库中看到.

Hi Everyone I am doing a project in which i need to download file from a url.Now I am downloading file and saving that in Personal Folder and it is saving in App directory.I am getting that file through file path and displaying in web view.But if the file is image or Video i cannot see that in Photo albums or gallery of my ipad.

你们中的任何人都可以帮助解决这个问题.

Can anyone of you please help in resolving that problem.

还有一件事,我在哪里可以找到通过 iPhone 或 iPad 保存在个人文件夹中的文档和 pdf?

And one more thing where can i find the documents and pdfs that are saved in Personal folder through iPhone or iPad?

提前致谢.

推荐答案

您只需将照片保存到个人文件夹,这是 iOS 上的沙盒文件夹.您可以使用依赖服务将图像写入系统相册.

You just save photos to Personal Folder, this is a sandbox folder on iOS. You can use dependency service to write images to system's album.

首先在PCL中创建一个接口:

Firstly create an interface in PCL:

public interface ISavePhotosToAlbum
{
    void SavePhotosWithStream(Stream stream);
}

图片下载后,用这个写成native:

After downloading the image, use this to write in native:

HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(urlStr);
HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();

DependencyService.Get<ISavePhotosToAlbum>().SavePhotosWithStream(aResponse.GetResponseStream());

最后我们应该在原生平台上实​​现这个Denpendency Service:

At last we should implement this Denpendency Service on native platform:

[assembly: Dependency(typeof(SaveToAlbum))]
namespace SavePhotosDemo.iOS
{
    public class SaveToAlbum : ISavePhotosToAlbum
    {
        public void SavePhotosWithStream(Stream stream)
        {
            var imageData = NSData.FromStream(stream);
            var image = UIImage.LoadFromData(imageData);

            image.SaveToPhotosAlbum((img, error) =>
            {

            });
        }
    }
}

请注意:在iOS上,当您想将照片写入相册时,我们需要在info.plist中添加Privacy.在 iOS 11 添加 NSPhotoLibraryAddUsageDescription,iOS 10 添加 NSPhotoLibraryUsageDescription.

Please notice that: on iOS when you want to write photos to album, we need to add Privacy in info.plist. On iOS 11 add NSPhotoLibraryAddUsageDescription, iOS 10 add NSPhotoLibraryUsageDescription.

这篇关于使用 Xamarin Forms 在 iPhone 或 iPad 的相册中保存照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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