c# - 如何在 ios 11 中保存到照片相册崩溃?

标签 c# ios xamarin xamarin.ios

当我们运行下面提到的代码来下载图像并将其保存在照片库中时,它会在 iOS 11 中自动崩溃。请指导解决此问题。

try 
{
    string uri = "https://www.xamarin.com/content/images/pages/branding/assets/xamagon.png";
    using (var url = new NSUrl(uri))
    using (var data = NSData.FromUrl(url))
    UIImage.LoadFromData(data).SaveToPhotosAlbum((image, error) => 
    {
        var o = image as UIImage;
        Console.WriteLine("error:" + error);
    });
} 
catch(Exception exx) 
{
    throw exx;
}

最佳答案

Swift - 4, Xcode 9, IOS 11

信息.plist

  <key>NSPhotoLibraryUsageDescription</key>
  <string> photos description.</string>

  <key>NSPhotoLibraryAddUsageDescription</key>
  <string> photos add description.</string>

在按钮点击中添加代码

let url = "https://www.xamarin.com/content/images/pages/branding/assets/xamagon.png"
        let data = try! Data(contentsOf: URL(string: url)!)

        let image:UIImage = UIImage(data: data)!

        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        if let error = error {
            // we got back an error!
            let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        } else {

            let ac = UIAlertController(title: "Saved!", message: "The image has been saved to your photos.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        }
    }

关于c# - 如何在 ios 11 中保存到照片相册崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50582489/

相关文章:

iOS8 - 在 UINavigationController 上推送 UIViewController 并旋转设备后,以前的 View Controller 中的大小错误

c# - Xamarin.Android SQLite SQLiteConnection.Insert 不返回插入对象的 ID

c# - 访问属性

iphone - UIActivityController 在 Facebook 上发布

ios - 使用 if 语句更改单元格中标签的文本颜色

android - 将数据从一个 xamarin.android 应用程序发送到另一个 xamarin.forms 应用程序

c# - 在 Xamarin.Forms 中,如何从项目文件夹中读取 Assets 列表?

c# - ASP.NET Core 2 - GetUserIdAsync 返回带有 Guid Id 的 null 结果

c# - 安全正确的结构编码

c# - 如何在点击按钮时显示通知提示? Windows 手机