-
-
Save icebeam7/52c8ffde6b21a83614cb2a8a5e806d11 to your computer and use it in GitHub Desktop.
Servicios/ServicioImagen.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Xamarin.Essentials; | |
namespace ICTalkMobileApp.Services | |
{ | |
public static class ServicioStorage | |
{ | |
public static async Task<string> SeleccionarFoto() | |
{ | |
var foto = await MediaPicker.PickPhotoAsync(new MediaPickerOptions() | |
{ | |
Title = "Elige una imagen" | |
}); | |
return await CopiarFoto(foto); | |
} | |
public static async Task<string> TomarFoto() | |
{ | |
if (MediaPicker.IsCaptureSupported) | |
{ | |
var foto = await MediaPicker.CapturePhotoAsync(new MediaPickerOptions() | |
{ | |
Title = "Toma la foto" | |
}); | |
return await CopiarFoto(foto); | |
} | |
return string.Empty; | |
} | |
private static async Task<string> CopiarFoto(FileResult foto) | |
{ | |
if (foto != null) | |
{ | |
var nombreCopia = $"{Guid.NewGuid()}{Path.GetExtension(foto.FileName)}"; | |
var rutaCopia = Path.Combine(FileSystem.CacheDirectory, nombreCopia); | |
using (var stream = await foto.OpenReadAsync()) | |
{ | |
using (var newStream = File.OpenWrite(rutaCopia)) | |
{ | |
await stream.CopyToAsync(newStream); | |
} | |
} | |
return rutaCopia; | |
} | |
return string.Empty; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment