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

Commit

Permalink
Refractor iOS extension handling and copying.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Oct 11, 2019
1 parent 49a339d commit 335fefa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Media.Plugin/iOS/ECLImagePickerViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)

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

var image = new UIImage(cgImage, 1.0f, (UIImageOrientation)rep.Orientation);
cgImage?.Dispose();
Expand Down
6 changes: 3 additions & 3 deletions src/Media.Plugin/iOS/MediaImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,14 @@ private static MediaPickerController SetupController(MediaPickerDelegate mpDeleg
var files = t.Result;
Parallel.ForEach(files, mediaFile =>
{
ResizeAndCompressImage(options, mediaFile);
ResizeAndCompressImage(options, mediaFile, Path.GetExtension(mediaFile.Path));
});

return t;
}).Unwrap();
}

private static void ResizeAndCompressImage(StoreCameraMediaOptions options, MediaFile mediaFile)
private static void ResizeAndCompressImage(StoreCameraMediaOptions options, MediaFile mediaFile, string pathExtension)
{
var image = UIImage.FromFile(mediaFile.Path);
var percent = 1.0f;
Expand Down Expand Up @@ -459,7 +459,7 @@ private static void ResizeAndCompressImage(StoreCameraMediaOptions options, Medi
var quality = (options.CompressionQuality / 100f);
var savedImage = false;
if (meta != null)
savedImage = MediaPickerDelegate.SaveImageWithMetadata(image, quality, meta, mediaFile.Path);
savedImage = MediaPickerDelegate.SaveImageWithMetadata(image, quality, meta, mediaFile.Path, pathExtension);

if (!savedImage)
image.AsJPEG(quality).Save(mediaFile.Path, true);
Expand Down
46 changes: 24 additions & 22 deletions src/Media.Plugin/iOS/MediaPickerDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace Plugin.Media
{
internal class MediaPickerDelegate : UIImagePickerControllerDelegate
{
static string photoType;

internal MediaPickerDelegate(UIViewController viewController, UIImagePickerControllerSourceType sourceType,
StoreCameraMediaOptions options, CancellationToken token)
{
Expand Down Expand Up @@ -294,11 +292,11 @@ private async Task<MediaFile> GetPictureMediaFile(NSDictionary info)
if (image == null)
return null;

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

var path = GetOutputPath(MediaImplementation.TypeImage,
options.Directory ?? ((IsCaptured) ? string.Empty : "temp"),
options.Name);
options.Name, pathExtension);

var cgImage = image.CGImage;

Expand Down Expand Up @@ -389,15 +387,15 @@ private async Task<MediaFile> GetPictureMediaFile(NSDictionary info)
}

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

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

//continue to move down quality , rare instances
while (imageData == null && finalQuality > 0)
Expand Down Expand Up @@ -445,7 +443,7 @@ private async Task<MediaFile> GetPictureMediaFile(NSDictionary info)
Func<Stream> getStreamForExternalStorage = () =>
{
if (options.RotateImage)
return RotateImage(image, options.CompressionQuality);
return RotateImage(image, options.CompressionQuality, pathExtension);
else
return File.OpenRead(path);
};
Expand Down Expand Up @@ -474,12 +472,13 @@ internal static NSDictionary SetGpsLocation(NSDictionary meta, Location location
return newMeta;
}

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

//continue to move down quality , rare instances
while (imageData == null && finalQuality > 0)
Expand Down Expand Up @@ -573,7 +572,7 @@ private async Task<MediaFile> GetMovieMediaFile(NSDictionary info)

var path = GetOutputPath(MediaImplementation.TypeMovie,
options?.Directory ?? ((IsCaptured) ? string.Empty : "temp"),
options?.Name ?? Path.GetFileName(url.Path));
options?.Name ?? Path.GetFileName(url.Path), url.PathExtension);

try
{
Expand All @@ -582,14 +581,14 @@ private async Task<MediaFile> GetMovieMediaFile(NSDictionary info)
catch (Exception ex)
{
Debug.WriteLine($"Unable to move file, trying to copy. {ex.Message}");
File.Copy(url.Path, path);
try
{
File.Copy(url.Path, path);
File.Delete(url.Path);
}
catch (Exception)
{
Debug.WriteLine($"Unable to delete file, will be left around :( {ex.Message}");
Debug.WriteLine($"Unable to copy/delete file, will be left around :( {ex.Message}");
}
}

Expand Down Expand Up @@ -620,11 +619,13 @@ private async Task<MediaFile> GetMovieMediaFile(NSDictionary info)
return new MediaFile(path, () => File.OpenRead(path), albumPath: aPath);
}

private static string GetUniquePath(string type, string path, string name)
private static string GetUniquePath(string type, string path, string name, string pathExtension)
{
var isPhoto = (type == MediaImplementation.TypeImage);
var ext = Path.GetExtension(name);
if (ext == string.Empty)
if (string.IsNullOrWhiteSpace(ext))
ext = pathExtension;
if(string.IsNullOrWhiteSpace(ext))
ext = ((isPhoto) ? ".jpg" : ".mp4");

name = Path.GetFileNameWithoutExtension(name);
Expand All @@ -637,7 +638,7 @@ private static string GetUniquePath(string type, string path, string name)
return Path.Combine(path, nname);
}

internal static string GetOutputPath(string type, string path, string name, long index = 0)
internal static string GetOutputPath(string type, string path, string name, string extension, long index = 0)
{
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), path);
Directory.CreateDirectory(path);
Expand All @@ -648,9 +649,9 @@ internal static string GetOutputPath(string type, string path, string name, long
if (string.IsNullOrWhiteSpace(name))
{
if (type == MediaImplementation.TypeImage)
name = photoType == "jpg" ? $"IMG_{postpendName}.jpg" : $"IMG_{postpendName}.png";
name = extension == "jpg" ? $"IMG_{postpendName}.jpg" : $"IMG_{postpendName}.png";
else
name = $"VID_{postpendName}.mp4";
name = $"VID_{postpendName}.{extension ?? "mp4"}";
}
else
{
Expand All @@ -662,7 +663,7 @@ internal static string GetOutputPath(string type, string path, string name, long
}
}

return Path.Combine(path, GetUniquePath(type, path, name));
return Path.Combine(path, GetUniquePath(type, path, name, extension));
}

private static bool IsValidInterfaceOrientation(UIDeviceOrientation self)
Expand Down Expand Up @@ -699,7 +700,7 @@ private static UIDeviceOrientation GetDeviceOrientation(UIInterfaceOrientation s
}
}

public static Stream RotateImage(UIImage image, int compressionQuality)
public static Stream RotateImage(UIImage image, int compressionQuality, string pathExtension)
{
UIImage imageToReturn = null;
if (image.Orientation == UIImageOrientation.Up)
Expand Down Expand Up @@ -783,8 +784,9 @@ public static Stream RotateImage(UIImage image, int compressionQuality)
}
}

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

0 comments on commit 335fefa

Please sign in to comment.