コード例 #1
0
        private MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
        {
            var rep = asset.DefaultRepresentation;

            if (rep == null)
            {
                return(null);
            }

            var cgImage = rep.GetImage();

            var path = MediaPickerDelegate.GetOutputPath(MediaImplementation.TypeImage,
                                                         _options.Directory ?? "temp",
                                                         _options.Name, index);

            var image = new UIImage(cgImage, 1.0f, (UIImageOrientation)rep.Orientation);

            cgImage?.Dispose();
            cgImage = null;
            rep?.Dispose();
            rep = null;

            image.AsJPEG().Save(path, true);

            image?.Dispose();
            image = null;
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Default);

            string aPath = null;
            //try to get the album path's url
            var url = asset.AssetUrl;

            aPath = url?.AbsoluteString;

            return(new MediaFile(path, () => File.OpenRead(path), albumPath: aPath));
        }
コード例 #2
0
        private MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
        {
            var rep = asset.DefaultRepresentation;

            if (rep == null)
            {
                return(null);
            }

            var cgImage = rep.GetImage();

            UIImage image = null;

            if (cgImage == null)
            {
                var fetch     = PHAsset.FetchAssets(new[] { asset.AssetUrl }, null);
                var ph        = fetch.firstObject as PHAsset;
                var manager   = PHImageManager.DefaultManager;
                var phOptions = new PHImageRequestOptions
                {
                    Version = PHImageRequestOptionsVersion.Original,
                    NetworkAccessAllowed = true,
                    Synchronous          = true
                };

                phOptions.ProgressHandler = (double progress, NSError error, out bool stop, NSDictionary info) =>
                {
                    Debug.WriteLine($"Progress: {progress.ToString()}");

                    stop = false;
                };

                if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
                {
                    manager.RequestImageDataAndOrientation(ph, phOptions, (data, i, orientation, k) =>
                    {
                        if (data != null)
                        {
                            image = new UIImage(data, 1.0f);
                        }
                    });
                }
                else
                {
                    manager.RequestImageData(ph, phOptions, (data, i, orientation, k) =>
                    {
                        if (data != null)
                        {
                            image = new UIImage(data, 1.0f);
                        }
                    });
                }
                phOptions?.Dispose();
                fetch?.Dispose();
                ph?.Dispose();
            }
            else
            {
                image = new UIImage(cgImage, 1.0f, (UIImageOrientation)rep.Orientation);
            }

            var path = MediaPickerDelegate.GetOutputPath(MediaImplementation.TypeImage,
                                                         options.Directory ?? "temp",
                                                         options.Name, asset.AssetUrl?.PathExtension, index);

            cgImage?.Dispose();
            cgImage = null;
            rep?.Dispose();
            rep = null;

            //There might be cases when the original image cannot be retrieved while image thumb was still present.
            //Then no need to try to save it as we will get an exception here
            //TODO: Ideally, we should notify the client that we failed to get original image
            //TODO: Otherwise, it might be confusing to the user, that he saw the thumb, but did not get the image
            if (image == null)
            {
                return(null);
            }

            image.AsJPEG().Save(path, true);

            image?.Dispose();
            image = null;
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Default);

            string aPath = null;
            //try to get the album path's url
            var url = asset.AssetUrl;

            aPath = url?.AbsoluteString;

            return(new MediaFile(path, () => File.OpenRead(path), albumPath: aPath));
        }
コード例 #3
0
        private MediaFile GetPictureMediaFile(ALAsset asset)
        {
            var rep = asset.DefaultRepresentation;

            if (rep == null)
            {
                return(null);
            }

            var cgImage = rep.GetImage();

            var path = MediaPickerDelegate.GetOutputPath(MediaImplementation.TypeImage,
                                                         _options.Directory ?? "temp",
                                                         _options.Name);

            var image = new UIImage(cgImage, 1.0f, (UIImageOrientation)rep.Orientation);

            var percent = 1.0f;

            if (_options.PhotoSize != PhotoSize.Full)
            {
                try
                {
                    switch (_options.PhotoSize)
                    {
                    case PhotoSize.Large:
                        percent = .75f;
                        break;

                    case PhotoSize.Medium:
                        percent = .5f;
                        break;

                    case PhotoSize.Small:
                        percent = .25f;
                        break;

                    case PhotoSize.Custom:
                        percent = (float)_options.CustomPhotoSize / 100f;
                        break;
                    }

                    if (_options.PhotoSize == PhotoSize.MaxWidthHeight && _options.MaxWidthHeight.HasValue)
                    {
                        var max = Math.Max(image.CGImage.Width, image.CGImage.Height);
                        if (max > _options.MaxWidthHeight.Value)
                        {
                            percent = (float)_options.MaxWidthHeight.Value / (float)max;
                        }
                    }

                    if (percent < 1.0f)
                    {
                        //begin resizing image
                        image = image.ResizeImageWithAspectRatio(percent);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Unable to compress image: {ex}");
                }
            }


            NSDictionary meta = null;

            try
            {
                //meta = PhotoLibraryAccess.GetPhotoLibraryMetadata(asset.AssetUrl);

                //meta = info[UIImagePickerController.MediaMetadata] as NSDictionary;
                if (meta != null && meta.ContainsKey(ImageIO.CGImageProperties.Orientation))
                {
                    var newMeta = new NSMutableDictionary();
                    newMeta.SetValuesForKeysWithDictionary(meta);
                    var newTiffDict = new NSMutableDictionary();
                    newTiffDict.SetValuesForKeysWithDictionary(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary);
                    newTiffDict.SetValueForKey(meta[ImageIO.CGImageProperties.Orientation], ImageIO.CGImageProperties.TIFFOrientation);
                    newMeta[ImageIO.CGImageProperties.TIFFDictionary] = newTiffDict;

                    meta = newMeta;
                }
                var location = _options.Location;
                if (meta != null && location != null)
                {
                    meta = MediaPickerDelegate.SetGpsLocation(meta, location);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to get metadata: {ex}");
            }

            //iOS quality is 0.0-1.0
            var quality    = (_options.CompressionQuality / 100f);
            var savedImage = false;

            if (meta != null)
            {
                savedImage = MediaPickerDelegate.SaveImageWithMetadata(image, quality, meta, path);
            }

            if (!savedImage)
            {
                image.AsJPEG(quality).Save(path, true);
            }


            string aPath = null;
            //try to get the album path's url
            var url = asset.AssetUrl;

            aPath = url?.AbsoluteString;

            return(new MediaFile(path, () => File.OpenRead(path), albumPath: aPath));
        }
コード例 #4
0
        private MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
        {
            var rep = asset.DefaultRepresentation;

            if (rep == null)
            {
                return(null);
            }

            var cgImage = rep.GetImage();

            UIImage image = null;

            if (cgImage == null)
            {
                var fetch     = PHAsset.FetchAssets(new[] { asset.AssetUrl }, null);
                var ph        = fetch.firstObject as PHAsset;
                var manager   = PHImageManager.DefaultManager;
                var phOptions = new PHImageRequestOptions
                {
                    Version = PHImageRequestOptionsVersion.Original,
                    NetworkAccessAllowed = true,
                    Synchronous          = true
                };

                phOptions.ProgressHandler = (double progress, NSError error, out bool stop, NSDictionary info) =>
                {
                    Debug.WriteLine($"Progress: {progress.ToString()}");

                    stop = false;
                };

                manager.RequestImageData(ph, phOptions, (data, i, orientation, k) =>
                {
                    if (data != null)
                    {
                        image = new UIImage(data, 1.0f);
                    }
                });
                phOptions?.Dispose();
                fetch?.Dispose();
                ph?.Dispose();
            }
            else
            {
                image = new UIImage(cgImage, 1.0f, (UIImageOrientation)rep.Orientation);
            }

            var path = MediaPickerDelegate.GetOutputPath(MediaImplementation.TypeImage,
                                                         options.Directory ?? "temp",
                                                         options.Name, asset.AssetUrl?.PathExtension, index);

            cgImage?.Dispose();
            cgImage = null;
            rep?.Dispose();
            rep = null;

            image.AsJPEG().Save(path, true);

            image?.Dispose();
            image = null;
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Default);

            string aPath = null;
            //try to get the album path's url
            var url = asset.AssetUrl;

            aPath = url?.AbsoluteString;

            return(new MediaFile(path, () => File.OpenRead(path), albumPath: aPath));
        }