我正在使用Xam.Plugin.Media来拍照。
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
{
Directory = "attachments",
Name = fileName,
CompressionQuality = 35
});
cam.Source = ImageSource.FromFile(file.Path);
上面的代码确实有效!
文件路径是(file.Path):
/var/mobile/Containers/Data/Application/F3997E36-78EB-41AF-A37F-FC794BAF30EC/Documents/attachments/13c8ac4e57734a36bded2c2694e27495.jpg
但此代码不显示图像控件中的图片
var q = "/var/mobile/Containers/Data/Application/F3997E36-78EB-41AF-A37F-FC794BAF30EC/Documents/attachments/13c8ac4e57734a36bded2c2694e27495.jpg"
cam.Source = ImageSource.FromFile(q);
您需要在图库中保存照片。当您的用户拍摄照片时,它仍会存储临时数据,但如果需要,还可以复制到公共图库
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
SaveToAlbum = true
});
获取公共相册路径
var aPpath = file.AlbumPath;
获得私人路径
var path = file.Path;
在iOS上,这是加载之前保存的文件的方法。这是因为每次重新启动应用程序时系统都会重新生成UDID。
public Stream LoadSampleStream(string filename) {
try
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filePath = Path.Combine(documentsPath, "Sample", filename);
return new FileStream(filePath, FileMode.Open);
}
catch(Exception ex) {
return null;
}
}