How to get videos with PHVideo from iCloud on Xamarin iOS?

265 Views Asked by At

I implemented multimedia picker (for images and videos) with DependencyService on Xamarin.Forms app. I have a problem on iCloud videos. When I select video that is on icloud, I can't get it.

My implementation is:

async void FinishedPickingAssets(object sender, MultiAssetEventArgs args)
         {
             IList<MediaFile> results = new List<MediaFile>();
             TaskCompletionSource<IList<MediaFile>> tcs = new TaskCompletionSource<IList<MediaFile>>();
    
             try 
             { 
                 var options = new PHImageRequestOptions()
                 {
                     NetworkAccessAllowed = true
                 };
    
                 options.Synchronous = false;
                 options.ResizeMode = PHImageRequestOptionsResizeMode.Fast;
                 options.DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat;
                 bool completed = false;
                 for (var i = 0; i < args.Assets.Length; i++)
                 {
                     var asset = args.Assets[i];
    
                     string fileName = string.Empty;
                     if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
                     {
                         fileName = PHAssetResource.GetAssetResources(asset).FirstOrDefault().OriginalFilename;
                         fileName = Path.GetFileNameWithoutExtension(fileName) + "(" + i.ToString() + ")" + Path.GetExtension(fileName);
                     }
    
                     switch (asset.MediaType)
                     {
                         case PHAssetMediaType.Video:
                         {
                                 var vOptions = new PHVideoRequestOptions();
                                 vOptions.NetworkAccessAllowed = true;
                                 vOptions.Version = PHVideoRequestOptionsVersion.Original;
                                 vOptions.DeliveryMode = PHVideoRequestOptionsDeliveryMode.Automatic;
                                 string videoUrl = "";
    
                                 PHImageManager.DefaultManager.RequestAvAsset(asset, vOptions, (avAsset, audioMix, vInfo) => 
                                 {
                                     DispatchQueue.MainQueue.DispatchAsync(() => {
    
                                         var error = vInfo.ObjectForKey(PHImageKeys.Error);
                                            
                                         if (avAsset != null)
                                         {
                                             videoUrl = ((AVFoundation.AVUrlAsset)avAsset).Url.Path;
                                         }
                                         var meFile = new MediaFile()
                                         {
                                             FileName = fileName,
                                             FilePath = videoUrl
                                         };
    
                                         using (Stream source = File.OpenRead(videoUrl))
                                         {
                                             meFile.FileSize = source.Length;
                                         }
                                         results.Add(meFile);
                                         OnMediaPicked?.Invoke(this, meFile);
    
                                         if (args.Assets.Length == results.Count && !completed)
                                         {
                                             completed = true;
                                             tcs.TrySetResult(results);
                                         }
                                     });
                                 });
                         }
                         break;
                     default:
                         PHImageManager.DefaultManager.RequestImageData(asset, options, (data, dataUti, orientation, info) =>
                         {
    
                             string path = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, fileName);
    
                             if (!File.Exists(path))
                             {
                                 Debug.WriteLine(dataUti);
                                 var imageData = data;
                                 imageData?.Save(path, true);
                             }
    
                             var meFile = new MediaFile()
                             {
                                 FileName = fileName,
                                 FilePath = path,
                                 FileSize = File.ReadAllBytes(path).Length
                             };
    
                             results.Add(meFile);
                             OnMediaPicked?.Invoke(this, meFile);
                             if (args.Assets.Length == results.Count && !completed)
                             {
                                 completed = true;
                                 tcs.TrySetResult(results);
                             }
    
                         });
                         break;
                     }
                 }
             }
             catch
             {
                 tcs.TrySetResult(results);
    
                 string automationId = "UnexpectedSituationMessage";
                 GeneralOperations.OpenToastMessage("Unexpected Situation", automationId);
             }
    
             mediaPickTcs?.TrySetResult(await tcs.Task);
         }

When I try to select iCloud object, I get following error: enter image description here

How can I take videos with multiple select from icloud?

Thank you in advance.

1

There are 1 best solutions below

5
Lucas Zhang On

Firstly make sure that you had turn on iCloud Photos .


 1. Go to Settings > [your name] > iCloud.
 2. Tap Photos.
 3. If you need to, tap to turn on iCloud Photos.

enter image description here

Then check if the video and photo display in Photo App on your device .

For multiple selection and several customizations you could install the plugin GMImagePicker.Xamarin from nuget .