// Requests all image of the assetCollections to be fetched.
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var assetCollection = AssetCollection;

            if (assetCollection == null)
            {
                return;
            }

            var items = PHAsset.FetchKeyAssets(assetCollection, new PHFetchOptions())
                        .Cast <PHAsset> ();

            assets.AddRange(items);
        }
Exemple #2
0
        private void UpdateFetchResults()
        {
            //What I do here is fetch both the albums list and the assets of each album.
            //This way I have acces to the number of items in each album, I can load the 3
            //thumbnails directly and I can pass the fetched result to the gridViewController.
            _collectionsFetchResultsAssets = null;
            _collectionsFetchResultsTitles = null;

            //Fetch PHAssetCollections:
            var topLevelUserCollections = _collectionsFetchResults[0];
            var smartAlbums             = _collectionsFetchResults[1];

            // All album: Sorted by descending creation date.
            var allFetchResults      = new List <PHFetchResult>();
            var allFetchResultLabels = new List <string> ();

            var options = new PHFetchOptions {
                Predicate       = NSPredicate.FromFormat("mediaType in %@", ToNSArray(_picker.MediaTypes)),
                SortDescriptors = new [] { new NSSortDescriptor("creationDate", _picker.GridSortOrder == SortOrder.Ascending) },
            };
            var assetsFetchResult = PHAsset.FetchAssets(options);

            allFetchResults.Add(assetsFetchResult);
            allFetchResultLabels.Add("picker.table.all-photos-label".Translate(defaultValue: "All photos"));

            //User albums:
            var userFetchResults      = new List <PHFetchResult>();
            var userFetchResultLabels = new List <string> ();

            foreach (PHCollection collection in topLevelUserCollections)
            {
                if (collection is PHAssetCollection)
                {
                    var collectionOptions = new PHFetchOptions {
                        Predicate       = NSPredicate.FromFormat("mediaType in %@", ToNSArray(_picker.MediaTypes)),
                        SortDescriptors = new[] { new NSSortDescriptor("creationDate", _picker.GridSortOrder == SortOrder.Ascending) },
                    };
                    var assetCollection = (PHAssetCollection)collection;

                    //Albums collections are always PHAssetCollectionType=1 & PHAssetCollectionSubtype=2
                    var collectionAssetsFetchResult = PHAsset.FetchKeyAssets(assetCollection, collectionOptions);
                    userFetchResults.Add(collectionAssetsFetchResult);
                    userFetchResultLabels.Add(collection.LocalizedTitle);
                }
            }

            //Smart albums: Sorted by descending creation date.
            var smartFetchResults      = new List <PHFetchResult>();
            var smartFetchResultLabels = new List <string> ();

            foreach (PHCollection collection in smartAlbums)
            {
                if (collection is PHAssetCollection)
                {
                    var assetCollection = (PHAssetCollection)collection;

                    //Smart collections are PHAssetCollectionType=2;
                    if (_picker.CustomSmartCollections != null && _picker.CustomSmartCollections.Contains(assetCollection.AssetCollectionSubtype))
                    {
                        var smartFetchOptions = new PHFetchOptions {
                            Predicate       = NSPredicate.FromFormat("mediaType in %@", ToNSArray(_picker.MediaTypes)),
                            SortDescriptors = new [] { new NSSortDescriptor("creationDate", _picker.GridSortOrder == SortOrder.Ascending) },
                        };

                        var smartAssetsFetchResult = PHAsset.FetchKeyAssets(assetCollection, smartFetchOptions);
                        if (smartAssetsFetchResult.Any())
                        {
                            smartFetchResults.Add(smartAssetsFetchResult);
                            smartFetchResultLabels.Add(collection.LocalizedTitle);
                        }
                    }
                }
            }

            _collectionsFetchResultsAssets = new PHFetchResult[][] {
                allFetchResults.ToArray(),
                    userFetchResults.ToArray(),
                    smartFetchResults.ToArray()
            };
            _collectionsFetchResultsTitles = new string[][] {
                allFetchResultLabels.ToArray(),
                    userFetchResultLabels.ToArray(),
                    smartFetchResultLabels.ToArray()
            };
        }