Beispiel #1
0
        public Task <IEnumerable <MediaAsset> > Query(MediaTypes mediaTypes, DateTimeOffset date)
        {
            var tcs = new TaskCompletionSource <IEnumerable <MediaAsset> >();

            Task.Run(() =>
            {
                var filter = "creationDate >= %@ && " + GetFilter(mediaTypes);

                var fetchOptions = new PHFetchOptions
                {
                    IncludeHiddenAssets   = false,
                    IncludeAllBurstAssets = false,
                    Predicate             = NSPredicate.FromFormat(filter, (NSDate)date.LocalDateTime),
                    SortDescriptors       = new [] {
                        new NSSortDescriptor("creationDate", false)
                    }
                };

                var fetchAssets = PHAsset
                                  .FetchAssets(fetchOptions)
                                  .OfType <PHAsset>()
                                  .ToArray();

                var options = new PHAssetResourceRequestOptions
                {
                    NetworkAccessAllowed = false
                };


                var imageCache = new PHCachingImageManager();
                imageCache.StartCaching(
                    fetchAssets,
                    PHImageManager.MaximumSize,
                    PHImageContentMode.Default,
                    new PHImageRequestOptions
                {
                    NetworkAccessAllowed = false
                }
                    );

                var assets = new List <MediaAsset>(fetchAssets.Count());
                foreach (var asset in fetchAssets)
                {
                    //result.MediaType
                    //result.Hidden
                    //result.Duration
                    //result.LocalIdentifier
                    //result.Location;
                    //result.ModificationDate
                    //result.PixelHeight;
                    //result.PixelWidth;
                    //result.CreationDate

                    switch (asset.MediaType)
                    {
                    case PHAssetMediaType.Image:
                        imageCache.RequestImageData(
                            asset,
                            new PHImageRequestOptions
                        {
                        },
                            (data, fn, orientation, dict) => { }
                            );
                        break;

                    case PHAssetMediaType.Video:
                        imageCache.RequestAvAsset(
                            asset,
                            new PHVideoRequestOptions
                        {
                            NetworkAccessAllowed = false,
                            DeliveryMode         = PHVideoRequestOptionsDeliveryMode.HighQualityFormat
                        },
                            (ass, mix, dict) => { }
                            );
                        break;

                    case PHAssetMediaType.Audio:
                        break;
                    }
                }
                imageCache.StopCaching();
                tcs.SetResult(assets);
            });
            return(tcs.Task);
        }