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

Commit

Permalink
Merge pull request #751 from jgold6/master
Browse files Browse the repository at this point in the history
Fixes issue #264
  • Loading branch information
jamesmontemagno authored Oct 11, 2019
2 parents 3968f2d + 402e005 commit 7d26df5
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Media.Plugin/iOS/MediaPickerDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Plugin.Media
{
internal class MediaPickerDelegate : UIImagePickerControllerDelegate
{
static string photoType;

internal MediaPickerDelegate(UIViewController viewController, UIImagePickerControllerSourceType sourceType,
StoreCameraMediaOptions options, CancellationToken token)
{
Expand Down Expand Up @@ -288,9 +290,11 @@ private async Task<MediaFile> GetPictureMediaFile(NSDictionary info)
{
var image = (UIImage)info[UIImagePickerController.EditedImage] ?? (UIImage)info[UIImagePickerController.OriginalImage];

if (image == null)
if (image == null)
return null;

photoType = ((info[UIImagePickerController.ReferenceUrl] as NSUrl).PathExtension == "PNG") ? "png" : "jpg";

var path = GetOutputPath(MediaImplementation.TypeImage,
options.Directory ?? ((IsCaptured) ? string.Empty : "temp"),
options.Name);
Expand Down Expand Up @@ -384,15 +388,15 @@ private async Task<MediaFile> GetPictureMediaFile(NSDictionary info)
}

//iOS quality is 0.0-1.0
var quality = (options.CompressionQuality / 100f);
var quality = photoType == "jpg" ? (options.CompressionQuality / 100f) : 0f;
var savedImage = false;
if (meta != null)
savedImage = SaveImageWithMetadata(image, quality, meta, path);

if (!savedImage)
{
var finalQuality = quality;
var imageData = image.AsJPEG(finalQuality);
var imageData = photoType == "jpg" ? image.AsJPEG(finalQuality) : image.AsPNG();

//continue to move down quality , rare instances
while (imageData == null && finalQuality > 0)
Expand Down Expand Up @@ -474,7 +478,7 @@ internal static bool SaveImageWithMetadata(UIImage image, float quality, NSDicti
try
{
var finalQuality = quality;
var imageData = image.AsJPEG(finalQuality);
var imageData = photoType == "jpg" ? image.AsJPEG(finalQuality) : image.AsPNG();

//continue to move down quality , rare instances
while (imageData == null && finalQuality > 0)
Expand Down Expand Up @@ -628,7 +632,7 @@ internal static string GetOutputPath(string type, string path, string name, long
if (string.IsNullOrWhiteSpace(name))
{
if (type == MediaImplementation.TypeImage)
name = $"IMG_{postpendName}.jpg";
name = photoType == "jpg" ? $"IMG_{postpendName}.jpg" : $"IMG_{postpendName}.png";
else
name = $"VID_{postpendName}.mp4";
}
Expand Down Expand Up @@ -763,8 +767,8 @@ public static Stream RotateImage(UIImage image, int compressionQuality)
}
}

var finalQuality = compressionQuality / 100f;
var imageData = imageToReturn.AsJPEG(finalQuality);
var finalQuality = photoType == "jpg" ? (compressionQuality / 100f) : 0f;
var imageData = photoType == "jpg" ? imageToReturn.AsJPEG(finalQuality) : imageToReturn.AsPNG();
//continue to move down quality , rare instances
while (imageData == null && finalQuality > 0)
{
Expand Down

0 comments on commit 7d26df5

Please sign in to comment.