Get last image from Photos.app?

后端 未结 13 2056
萌比男神i
萌比男神i 2020-11-27 09:50

I have seen other apps do it where you can import the last photo from the Photos app for quick use but as far as I know, I only know how to get A image and not the last (mos

13条回答
  •  执笔经年
    2020-11-27 10:16

    Xamarin.iOS version of accepted answer (how to get last image) including all notices from other answers:

      private void ChooseLastTakenPictureImplementation()
        {
            var library = new ALAssetsLibrary();
            // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
            library.Enumerate(ALAssetsGroupType.SavedPhotos, (ALAssetsGroup assetsGroup, ref bool stop) =>
                {
                    if (stop || assetsGroup == null)
                    {
                        return;
                    }
                    //Xamarin does not support ref parameters in nested lamba expressions
                    var lambdaStop = false;
                    //Check that the group has more than one picture
                    if (assetsGroup.Count > 0)
                    {
                        // Within the group enumeration block, filter to enumerate just photos.
                        assetsGroup.SetAssetsFilter(ALAssetsFilter.AllPhotos);
                        // Chooses the photo at the last index
                        assetsGroup.Enumerate(NSEnumerationOptions.Reverse, (ALAsset result, int index, ref bool innerStop) =>
                            {
                                // The end of the enumeration is signaled by asset == nil.
                                if (result != null)
                                {
                                    var representation = result.DefaultRepresentation;
                                    var latestPhoto = new UIImage(representation.GetImage(), representation.Scale, (UIImageOrientation)representation.Orientation);
                                    // Stop the enumerations
                                    lambdaStop = innerStop = true;
                                    // Do something interesting with the AV asset.
                                    HandleImageAutoPick(latestPhoto);
                                }
                            });
                        stop = lambdaStop;
                        return;
                    }
                    else
                    {
                        //Handle this special case where user has no pictures
                    }
                }, error =>
                {
                    // Typically you should handle an error more gracefully than this.
                    Debug.WriteLine(error.Description);
                });
        }
    

提交回复
热议问题