Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Attempt to fix #747 and #760
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Nov 21, 2019
1 parent 7942bd9 commit abd2177
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Media.Plugin/iOS/MediaPickerDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Threading;
using System.Diagnostics;
using System.Drawing;
using CoreImage;

namespace Plugin.Media
{
Expand Down Expand Up @@ -468,8 +469,47 @@ internal static NSDictionary SetGpsLocation(NSDictionary meta, Location location
return newMeta;
}

internal static bool SaveImageWithMetadataiOS13(UIImage image, float quality, NSDictionary meta, string path, string pathExtension)
{
try
{
pathExtension = pathExtension.ToLowerInvariant();
var finalQuality = quality;
var imageData = pathExtension == "jpg" ? image.AsJPEG(finalQuality) : image.AsPNG();

//continue to move down quality , rare instances
while (imageData == null && finalQuality > 0)
{
finalQuality -= 0.05f;
imageData = image.AsJPEG(finalQuality);
}

if (imageData == null)
throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or lower quality level");

// Copy over meta data
using var ciImage = CIImage.FromData(imageData);
using var newImageSource = ciImage.CreateBySettingProperties(meta);
using var ciContext = new CIContext();

if (pathExtension == "jpg")
return ciContext.WriteJpegRepresentation(newImageSource, NSUrl.FromFilename(path), CGColorSpace.CreateSrgb(), new NSDictionary(), out var error);

return ciContext.WritePngRepresentation(newImageSource, NSUrl.FromFilename(path), CIFormat.ARGB8, CGColorSpace.CreateSrgb(), new NSDictionary(), out var error2);
}
catch (Exception ex)
{
Console.WriteLine($"Unable to save image with metadata: {ex}");
}

return false;
}

internal static bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path, string pathExtension)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
return SaveImageWithMetadataiOS13(image, quality, meta, path, pathExtension);

try
{
pathExtension = pathExtension.ToLowerInvariant();
Expand Down

0 comments on commit abd2177

Please sign in to comment.