"Save Photos in Photo albums of iPhone or iPad using Xamarin Forms" 但是 PCL 已弃用
"Save Photos in Photo albums of iPhone or iPad using Xamarin Forms" However PCL is Deprecated
我正在尝试解决 Stack Overflow Post“ post PCL 的答案中已弃用。什么是更新的答案?谢谢。
However in the answer for the post PCL is deprecated.
实际上,您只需要在共享项目中做同样的事情。 DependencyService 在 Xamarin.Forms 中可用,您可以参考 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction .
在表格中
public interface ISavePhotosToAlbum
{
void SavePhotosWithStream(string name, Stream data);
}
在iOS
[assembly: Dependency(typeof(SaveToAlbum))]
namespace xxx.iOS
{
public class SaveToAlbum : ISavePhotosToAlbum
{
public void SavePhotosWithStream(string name,Stream stream)
{
var imageData = NSData.FromStream(stream);
var image = UIImage.LoadFromData(imageData);
image.SaveToPhotosAlbum((img, error) =>
{
});
}
}
}
并在info.plist
中添加如下代码
<key>NSCameraUsageDescription</key>
<string>App need to access your camera</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>App need to access your albums</string> // new in iOS 11 and later
<key>NSPhotoLibraryUsageDescription</key>
<string>App need to access your albums</string>
此外,如果您想在Android
中实现它
在Android
[assembly: Dependency(typeof(SaveToAlbum))]
namespace xxx.Droid
{
public class SaveToAlbum : ISavePhotosToAlbum
{
public void SavePhotosWithStream(string name,Stream stream)
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documentsPath = Path.Combine(documentsPath, "Orders", temp);
Directory.CreateDirectory(documentsPath);
string filePath = Path.Combine(documentsPath, name);
byte[] bArray = new byte[data.Length];
using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
{
using (data)
{
data.Read(bArray, 0, (int)data.Length);
}
int length = bArray.Length;
fs.Write(bArray, 0, length);
}
}
}
}
我正在尝试解决 Stack Overflow Post“
However in the answer for the post PCL is deprecated.
实际上,您只需要在共享项目中做同样的事情。 DependencyService 在 Xamarin.Forms 中可用,您可以参考 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction .
在表格中
public interface ISavePhotosToAlbum
{
void SavePhotosWithStream(string name, Stream data);
}
在iOS
[assembly: Dependency(typeof(SaveToAlbum))]
namespace xxx.iOS
{
public class SaveToAlbum : ISavePhotosToAlbum
{
public void SavePhotosWithStream(string name,Stream stream)
{
var imageData = NSData.FromStream(stream);
var image = UIImage.LoadFromData(imageData);
image.SaveToPhotosAlbum((img, error) =>
{
});
}
}
}
并在info.plist
中添加如下代码<key>NSCameraUsageDescription</key>
<string>App need to access your camera</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>App need to access your albums</string> // new in iOS 11 and later
<key>NSPhotoLibraryUsageDescription</key>
<string>App need to access your albums</string>
此外,如果您想在Android
中实现它在Android
[assembly: Dependency(typeof(SaveToAlbum))]
namespace xxx.Droid
{
public class SaveToAlbum : ISavePhotosToAlbum
{
public void SavePhotosWithStream(string name,Stream stream)
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documentsPath = Path.Combine(documentsPath, "Orders", temp);
Directory.CreateDirectory(documentsPath);
string filePath = Path.Combine(documentsPath, name);
byte[] bArray = new byte[data.Length];
using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
{
using (data)
{
data.Read(bArray, 0, (int)data.Length);
}
int length = bArray.Length;
fs.Write(bArray, 0, length);
}
}
}
}