async void RequestAccessToPhotoLibrary()
        {
            switch (PHPhotoLibrary.AuthorizationStatus)
            {
            case PHAuthorizationStatus.NotDetermined:
                await PHPhotoLibrary.RequestAuthorizationAsync();

                GetPhotos();
                break;

            case PHAuthorizationStatus.Authorized:
                break;

            default:
                UIAlertHelper.ShowMessage("Cannot access to Photo Library!",
                                          "Please, check in Settings app that you have permission to access to Photos.",
                                          NavigationController,
                                          "Ok",
                                          null,
                                          null,
                                          null,
                                          new [] { "Go to settings" },
                                          new Action[] { OpenSettings });
                break;
            }
        }
示例#2
0
        public async Task <string> PickFile()
        {
            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (status != PHAuthorizationStatus.Authorized)
            {
                return(null);
            }
            var taskCompletionSource = new TaskCompletionSource <string>();
            var imagePicker          = new UIImagePickerController();

            imagePicker.FinishedPickingMedia += (sender, args) => {
                taskCompletionSource.SetResult(args.ReferenceUrl?.AbsoluteString);
                imagePicker.DismissModalViewController(true);
            };
            imagePicker.Canceled += (sender, args) => {
                taskCompletionSource.SetResult(null);
                imagePicker.DismissModalViewController(true);
            };
            UIWindow window         = UIApplication.SharedApplication.KeyWindow;
            var      viewController = window.RootViewController;

            viewController.PresentModalViewController(imagePicker, true);
            return(await taskCompletionSource.Task);
        }
示例#3
0
        private async Task <bool> OnSaveSignature(Stream bitmap, string filename)
        {
            var tcs = new TaskCompletionSource <bool> ();

            UIImage image;

            using (var data = NSData.FromStream(bitmap))
            {
                image = UIImage.LoadFromData(data);
            }

            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (status == PHAuthorizationStatus.Authorized)
            {
                image.SaveToPhotosAlbum((i, error) =>
                {
                    image.Dispose();

                    tcs.TrySetResult(error == null);
                });
            }
            else
            {
                tcs.TrySetResult(false);
            }

            return(await tcs.Task);
        }
        private async void SetGalleryButton()
        {
            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (status == PHAuthorizationStatus.Authorized)
            {
                var fetchedAssets    = PHAsset.FetchAssets(PHAssetMediaType.Image, null);
                var lastGalleryPhoto = fetchedAssets.LastObject as PHAsset;
                if (lastGalleryPhoto != null)
                {
                    galleryButton.UserInteractionEnabled = true;
                    var PHImageManager = new PHImageManager();
                    PHImageManager.RequestImageForAsset(lastGalleryPhoto, new CGSize(300, 300),
                                                        PHImageContentMode.AspectFill, new PHImageRequestOptions()
                    {
                        DeliveryMode = PHImageRequestOptionsDeliveryMode.Opportunistic,
                        ResizeMode   = PHImageRequestOptionsResizeMode.Exact
                    }, (img, info) =>
                    {
                        galleryButton.Image = img;
                    });
                }
                else
                {
                    galleryButton.UserInteractionEnabled = false;
                }
            }
            else
            {
                galleryButton.UserInteractionEnabled = true;
            }
        }
        private async void SetGalleryButton()
        {
            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (status == PHAuthorizationStatus.Authorized)
            {
                var options = new PHFetchOptions
                {
                    SortDescriptors = new[] { new NSSortDescriptor("creationDate", false) },
                    FetchLimit      = 5,
                };

                var fetchedAssets    = PHAsset.FetchAssets(PHAssetMediaType.Image, options);
                var lastGalleryPhoto = fetchedAssets.FirstOrDefault() as PHAsset;
                if (lastGalleryPhoto != null)
                {
                    var PHImageManager = new PHImageManager();
                    PHImageManager.RequestImageForAsset(lastGalleryPhoto, new CGSize(300, 300),
                                                        PHImageContentMode.AspectFill, new PHImageRequestOptions(), (img, info) =>
                    {
                        galleryButton.Image = img;
                    });
                }
                else
                {
                    galleryButton.Image = UIImage.FromBundle("ic_noavatar");
                }
            }
        }
        async partial void SaveImageClicked(UIButton sender)
        {
            UIImage image;

            using (var bitmap = await signatureView.GetImageStreamAsync(SignatureImageFormat.Png, UIColor.Black, UIColor.White, 1f))
                using (var data = NSData.FromStream(bitmap))
                {
                    image = UIImage.LoadFromData(data);
                }

            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (status == PHAuthorizationStatus.Authorized)
            {
                image.SaveToPhotosAlbum((i, error) =>
                {
                    image.Dispose();

                    if (error == null)
                    {
                        ShowToast("Raster signature saved to the photo library.");
                    }
                    else
                    {
                        ShowToast("There was an error saving the signature: " + error.LocalizedDescription);
                    }
                });
            }
            else
            {
                ShowToast("Permission to save to the photo library was denied.");
            }
        }
示例#7
0
        protected override async Task StartListeningAsync()
        {
            await base.StartListeningAsync();

            if (await PHPhotoLibrary.RequestAuthorizationAsync() == PHAuthorizationStatus.Authorized)
            {
                PHPhotoLibrary.SharedPhotoLibrary.RegisterChangeObserver(_changeObserver);
            }
        }
示例#8
0
        public async Task <bool> RequestAuthorizationAsync()
        {
            if (IsAuthorized())
            {
                return(true);
            }

            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            return(status == PHAuthorizationStatus.Authorized);
        }
        public async Task <bool> RequestPermissionAsync()
        {
            var status = PHPhotoLibrary.AuthorizationStatus;

            bool authotization = status == PHAuthorizationStatus.Authorized;

            if (!authotization)
            {
                authotization = await PHPhotoLibrary.RequestAuthorizationAsync() == PHAuthorizationStatus.Authorized;
            }
            return(authotization);
        }
示例#10
0
        private async Task <bool> Import()
        {
            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (status != PHAuthorizationStatus.Authorized)
            {
                return(false);
            }
            results = PHAsset.FetchAssets(PHAssetMediaType.Image, null).Select(x => (PHAsset)x).ToArray();

            return(true);
        }
示例#11
0
        private async Task ValidatePhotoLibraryAccess()
        {
            if (!IsUsageKeyDefined("NSPhotoLibraryUsageDescription"))
            {
                throw new InvalidOperationException("Info.plist must define NSPhotoLibraryUsageDescription");
            }

            var isAllowed = (await PHPhotoLibrary.RequestAuthorizationAsync()) == PHAuthorizationStatus.Authorized;

            if (!isAllowed)
            {
                throw new UnauthorizedAccessException();
            }
        }
示例#12
0
        public async Task <IEnumerable <XMediaFile> > GetMediaFiles()
        {
            var images     = new List <XMediaFile>();
            var permission = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (permission == PHAuthorizationStatus.Authorized)
            {
                PHImageManager imageManager = PHImageManager.DefaultManager;

                var requestOptions = new PHImageRequestOptions()
                {
                    Synchronous = true
                };

                var results = PrepareResults();

                for (int i = 0; i < results.Count; i++)
                {
                    var asset = results.ObjectAt(i) as PHAsset;

                    imageManager.RequestImageForAsset(asset, new CGSize(100, 100), PHImageContentMode.AspectFill, requestOptions, (image, info) =>
                    {
                        var funcBytes = new Func <byte[]>(() =>
                        {
                            byte[] rawBytes = null;
                            using (NSData imageData = image.AsJPEG())
                            {
                                rawBytes = new Byte[imageData.Length];
                                System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, rawBytes, 0, (int)imageData.Length);
                            }

                            return(rawBytes);
                        });


                        images.Add(new XMediaFile()
                        {
                            Data      = funcBytes,
                            DateAdded = asset.CreationDate.ToDateTime().ToShortDate(),
                            MediaType = asset.MediaType.ToString()
                        });
                    });
                }

                return(images);
            }

            return(images);
        }
        /// <summary>Request <see cref="SaveMediaPermission"/> from the user.</summary>
        /// <returns>The status of the permission that was requested.</returns>
        public override async Task <PermissionStatus> RequestAsync()
        {
            var status = await CheckStatusAsync();

            if (status == PermissionStatus.Granted)
            {
                return(status);
            }

            var auth = MediaGallery.HasOSVersion(14)
                ? await PHPhotoLibrary.RequestAuthorizationAsync(PHAccessLevel.AddOnly)
                : await PHPhotoLibrary.RequestAuthorizationAsync();


            return(Convert(auth));
        }
示例#14
0
        private async void SetGalleryButton()
        {
            var status = await PHPhotoLibrary.RequestAuthorizationAsync();

            if (status == PHAuthorizationStatus.Authorized)
            {
                var options = new PHFetchOptions
                {
                    SortDescriptors = new[] { new NSSortDescriptor("creationDate", false) },
                    FetchLimit      = 5,
                };

                var fetchedAssets    = PHAsset.FetchAssets(PHAssetMediaType.Image, options);
                var lastGalleryPhoto = fetchedAssets.FirstOrDefault() as PHAsset;
                if (lastGalleryPhoto != null)
                {
                    galleryButton.UserInteractionEnabled = true;
                    var PHImageManager = new PHImageManager();
                    PHImageManager.RequestImageForAsset(lastGalleryPhoto, new CGSize(300, 300),
                                                        PHImageContentMode.AspectFill, new PHImageRequestOptions()
                    {
                        DeliveryMode = PHImageRequestOptionsDeliveryMode.Opportunistic,
                        ResizeMode   = PHImageRequestOptionsResizeMode.Exact
                    }, (img, info) =>
                    {
                        galleryButton.Image = img;
                    });
                }
                else
                {
                    galleryButton.UserInteractionEnabled = false;
                }
            }
            else
            {
                galleryButton.UserInteractionEnabled = true;
            }
        }
示例#15
0
        public async Task <AccessState> RequestAccess()
        {
            var status = PHPhotoLibrary.AuthorizationStatus;

            if (status == PHAuthorizationStatus.NotDetermined)
            {
                status = await PHPhotoLibrary.RequestAuthorizationAsync();
            }

            switch (status)
            {
            case PHAuthorizationStatus.Authorized:
                return(AccessState.Available);

            case PHAuthorizationStatus.Denied:
                return(AccessState.Denied);

            case PHAuthorizationStatus.Restricted:
                return(AccessState.Restricted);

            default:
                throw new ArgumentException("Invalid status - " + status);
            }
        }
示例#16
0
 static async Task <PermissionStatus> RequestPhotoPermissionStatus(PHAccessLevel level)
 => Convert(CheckOSVersionForPhotos()
                         ? await PHPhotoLibrary.RequestAuthorizationAsync(level)
                         : await PHPhotoLibrary.RequestAuthorizationAsync());
示例#17
0
 public static async void RequestPermission()
 {
     await PHPhotoLibrary.RequestAuthorizationAsync();
 }