如何在ios 11中保存ToPhotosAlbum崩溃?

问题描述 投票:0回答:2

当我们运行下面的代码提示下载并保存照片库中的图像时,它会在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;
}
c# ios xamarin xamarin.ios
2个回答
1
投票

Swift - 4,Xcode 9,IOS 11

的Info.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)
        }
    }

0
投票

你应该添加这个iOS 11更新

<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) would like to use Photo Library</string>
© www.soinside.com 2019 - 2024. All rights reserved.