我想从库中选择一张图片或用相机拍照并将结果显示给视图(ImageView)
但是根据包括这篇文章在内的一些帖子,我使用的 MvxHttpImageView 需要一个 Uri 来显示图像(无论它来自文件系统还是相机)。这意味着,将 Stream 转换为文件并取回 Uri。
我写了一个图片服务来完成这项工作:
public class PictureService : IPictureService,
IMvxServiceConsumer<IMvxPictureChooserTask>,
IMvxServiceConsumer<IMvxSimpleFileStoreService>
{
private const int MaxPixelDimension = 1024;
private const int DefaultJpegQuality = 92;
public void TakeNewPhoto(Action<string> onSuccess, Action<string> onError)
{
this.GetService<IMvxPictureChooserTask>().TakePicture(
PictureService.MaxPixelDimension,
PictureService.DefaultJpegQuality,
pictureStream =>
{
var newPictureUri = this.Save(pictureStream);
if (!string.IsNullOrWhiteSpace(newPictureUri))
onSuccess(newPictureUri);
else
onError("No picture selected");
},
() => { /* cancel is ignored */ });
}
public void SelectExistingPicture(Action<string> onSuccess, Action<string> onError)
{
this.GetService<IMvxPictureChooserTask>().ChoosePictureFromLibrary(
PictureService.MaxPixelDimension,
PictureService.DefaultJpegQuality,
pictureStream =>
{
var newPictureUri = this.Save(pictureStream);
if (!string.IsNullOrWhiteSpace(newPictureUri))
onSuccess(newPictureUri);
else
onError("No photo taken");
},
() => { /* cancel is ignored */ });
}
private string Save(Stream stream)
{
string fileName = null;
try
{
fileName = Guid.NewGuid().ToString("N");
var fileService = this.GetService<IMvxSimpleFileStoreService>();
fileService.WriteFile(fileName, stream.CopyTo);
}
catch (Exception)
{
fileName = null;
}
return fileName;
}
}
但出于隐私原因,我不想将图片保存在文件系统上。工作流程是:
- 拍摄或选择照片
- 在屏幕上显示(带有附加信息)
- 将我的模型保存在将图像发送到云的服务器上:不在设备上跟踪
我的问题是:如何在不保存文件系统的情况下处理包含图片数据的流?
或者
如何使用用户无法访问的临时存储系统(忽略“根”设备案例)?
谢谢你的帮助。