Ejemplo n.º 1
0
        private PHFetchResult AdjustAssets(PHFetchResult assets, PHFetchResultChangeDetails changes)
        {
            if (assets == null)
            {
                return(null);
            }

            var before = assets;

            assets = changes.FetchResultAfterChanges;

            foreach (var asset in before.OfType <PHAsset>())
            {
                if (!assets.Contains(asset))
                {
                    AllAssets.Remove(asset);
                }
            }

            foreach (var asset in assets.OfType <PHAsset>().OrderBy(a => a.CreationDate.SecondsSinceReferenceDate))
            {
                if (!AllAssets.Contains(asset))
                {
                    AllAssets.Insert(0, asset);
                }
            }

            return(assets);
        }
Ejemplo n.º 2
0
        private void OnAuthorized()
        {
            DispatchQueue.MainQueue.DispatchAsync(() =>
            {
                _imageManager = new PHCachingImageManager();

                var options = new PHFetchOptions
                {
                    SortDescriptors = new[] { new NSSortDescriptor("creationDate", false) }
                };

                var assets = new List <PHAsset>();

                if (_mediaTypes.HasFlag(MediaType.Image))
                {
                    _images = PHAsset.FetchAssets(PHAssetMediaType.Image, options);
                    assets.AddRange(_images.OfType <PHAsset>());
                }

                if (_mediaTypes.HasFlag(MediaType.Video))
                {
                    _videos = PHAsset.FetchAssets(PHAssetMediaType.Video, options);
                    assets.AddRange(_videos.OfType <PHAsset>());
                }

                foreach (var asset in assets.OrderByDescending(a => a.CreationDate.SecondsSinceReferenceDate))
                {
                    AllAssets.Add(asset);
                }

                ShowFirstImage();

                AllAssets.CollectionChanged += AssetsCollectionChanged;
                PHPhotoLibrary.SharedPhotoLibrary.RegisterChangeObserver(this);
            });
        }