当我们运行下面的代码提示下载并保存照片库中的图像时,它会在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
的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)
}
}
你应该添加这个iOS 11更新
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) would like to use Photo Library</string>