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)); }
static void ResizeAndCompressImage(StoreCameraMediaOptions options, MediaFile mediaFile, string pathExtension) { var image = UIImage.FromFile(mediaFile.Path); 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.Size.Width, image.Size.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; if (options.SaveMetaData) { try { meta = PhotoLibraryAccess.GetPhotoLibraryMetadata(new NSUrl(mediaFile.AlbumPath)); } 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, mediaFile.Path, pathExtension); } if (!savedImage) { if (pathExtension == "png") { image.AsPNG().Save(mediaFile.Path, true); } else { image.AsJPEG(quality).Save(mediaFile.Path, true); } } image?.Dispose(); image = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); }
private static void ResizeAndCompressImage(StoreCameraMediaOptions options, MediaFile mediaFile, string pathExtension) { var image = UIImage.FromFile(mediaFile.Path); 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.Size.Width, image.Size.Height); if (max > options.MaxWidthHeight.Value) { percent = (float)options.MaxWidthHeight.Value / (float)max; } } if (percent < 1.0f) { //begin resizing image image = image.ResizeImageWithAspectRatio(percent); } 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, mediaFile.Path, pathExtension); } if (!savedImage) { image.AsJPEG(quality).Save(mediaFile.Path, true); } image?.Dispose(); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); } catch (Exception ex) { Console.WriteLine($"Unable to compress image: {ex}"); } } }