Last active
October 17, 2020 13:50
-
-
Save dimonovdd/07ecb6ceb14bf72c7e54dc0210e4d0c0 to your computer and use it in GitHub Desktop.
SaveToGalery
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
. |
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
public async Task<bool> SaveImageAsync(byte[] data, string filename, string folder = null) | |
{ | |
try | |
{ | |
File picturesDirectory = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures); | |
File folderDirectory; | |
if (await Permissions.RequestAsync<Permissions.StorageWrite>() != PermissionStatus.Granted) | |
return false; | |
if (!string.IsNullOrEmpty(folder)) | |
{ | |
folderDirectory = new File(picturesDirectory, folder); | |
folderDirectory.Mkdirs(); | |
} | |
else | |
folderDirectory = picturesDirectory; | |
using (File bitmapFile = new File(folderDirectory, filename)) | |
{ | |
bitmapFile.CreateNewFile(); | |
using (FileOutputStream outputStream = new FileOutputStream(bitmapFile)) | |
await outputStream.WriteAsync(data); | |
MediaScannerConnection.ScanFile(Platform.CurrentActivity, | |
new string[] { bitmapFile.Path }, | |
new string[] { "image/png", "image/jpeg" }, null); | |
} | |
} | |
catch | |
{ | |
return false; | |
} | |
return true; | |
} |
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
public Task<bool> SaveImageAsync(byte[] data, string filename, string folder = null) | |
{ | |
NSData nsData = NSData.FromArray(data); | |
UIImage image = new UIImage(nsData); | |
TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>(); | |
image.SaveToPhotosAlbum((UIImage img, NSError error) => taskCompletionSource.SetResult(error == null)); | |
return taskCompletionSource.Task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment