public void SaveFileToPhotosAlbum(string filePath, byte[] fileData) { string ext = Path.GetExtension(filePath); if (ext.ToUpper() == ".MOV" || ext.ToUpper() == ".M4V") { File.WriteAllBytes(filePath, fileData); if (UIVideo.IsCompatibleWithSavedPhotosAlbum(filePath)) { UIVideo.SaveToPhotosAlbum(filePath, (path, error) => { if (error == null) { if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum(this, new FilesSavedToPhotosAlbumArgs(path, path)); } } else { Console.Out.WriteLine("Video {0} cannot be saved to photos album!", filePath); } }); } } else if (ext.ToUpper() == ".JPEG" || ext.ToUpper() == ".JPG" || ext.ToUpper() == ".PNG") { NSData imgData = NSData.FromArray(fileData); var img = UIImage.LoadFromData(imgData); var meta = new NSDictionary(); ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(img.CGImage, meta, (assetUrl, error) => { if (error == null) { if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum(this, new FilesSavedToPhotosAlbumArgs(assetUrl.ToString(), filePath)); } else { Console.Out.WriteLine("Image {0} cannot be saved to photos album!", filePath); } } }); img.Dispose(); } else { File.WriteAllBytes(filePath, fileData); if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum(this, new FilesSavedToPhotosAlbumArgs(filePath, filePath)); } } }
public static bool SaveVideoToGalery(NSUrl video, string path, string albumName) { var saved = true; var compatible = UIVideo.IsCompatibleWithSavedPhotosAlbum(path); var customAlbum = string.IsNullOrEmpty(albumName) ? null : FindOrCreateAlbum(albumName); if (!compatible) { return(false); } UIVideo.SaveToPhotosAlbum(path, (path, error) => { if (error != null) { saved = false; Console.WriteLine(error); } else if (customAlbum != null) { var savedAsset = (PHAsset)PHAsset.FetchAssets(PHAssetMediaType.Video, new PHFetchOptions { SortDescriptors = new[] { new NSSortDescriptor("creationDate", false) }, FetchLimit = 1 }).FirstOrDefault(); if (savedAsset != null) { PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => { var albumRequest = PHAssetCollectionChangeRequest.ChangeRequest(customAlbum); albumRequest?.AddAssets(new[] { savedAsset }); }, (success, error) => { if (!success) { Console.WriteLine(error); saved = success; } } ); } } }); return(saved); }