public override async void FinishedRecording(AVCaptureFileOutput captureOutput, NSUrl outputFileUrl, NSObject[] connections, NSError error)
        {
            if (UIVideo.IsCompatibleWithSavedPhotosAlbum(outputFileUrl.Path))
            {
                var library = new ALAssetsLibrary();
                library.WriteVideoToSavedPhotosAlbum(outputFileUrl, async(path, e2) =>
                {
                    if (e2 != null)
                    {
                        new UIAlertView("", e2.ToString(), null, "Error Occurred", null).Show();
                    }
                    else
                    {
                        view.activityIndicator.StopAnimating();
                        new UIAlertView("", "Saved to Photos", null, "Ok", null).Show();

                        //by using messaging center we can send data to portable CameraPage
                        NSData data      = NSData.FromUrl(outputFileUrl);
                        byte[] dataBytes = new byte[data.Length];
                        System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));
                        MessagingCenter.Send <string, byte[]>("VideoByteArrayReady", "ByteArrayIsReady", dataBytes);
                        MessagingCenter.Send <string, string>("VideoPathReady", "VideoPathReady", outputFileUrl.AbsoluteString);

                        await(element as CameraPage).Navigation.PopAsync();
                    }
                });
            }
            else
            {
                new UIAlertView("Incompatible", "Incompatible", null, "Ok", null).Show();
            }
        }
Example #2
0
        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);
        }