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 #792 from klogeaage/fix-png-issues-and-missing-met…
Browse files Browse the repository at this point in the history
…a-data-from-pickphotos

Made sure PNG is only used when image actually IS a PNG.
  • Loading branch information
jamesmontemagno authored Dec 22, 2019
2 parents b12d1d1 + c788d7d commit b197ac8
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 318 deletions.
89 changes: 50 additions & 39 deletions src/Media.Plugin/iOS/ECLImagePickerViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
phOptions.ProgressHandler = (double progress, NSError error, out bool stop, NSDictionary info) =>
{
Debug.WriteLine($"Progress: {progress.ToString()}");

stop = false;
};

Expand All @@ -190,7 +190,7 @@ private MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
});
}
else
{
{
manager.RequestImageData(ph, phOptions, (data, i, orientation, k) =>
{
if (data != null)
Expand All @@ -206,25 +206,33 @@ private MediaFile GetPictureMediaFile(ALAsset asset, long index = 0)
image = new UIImage(cgImage, 1.0f, (UIImageOrientation)rep.Orientation);
}

var path = MediaPickerDelegate.GetOutputPath(MediaImplementation.TypeImage,
string path = MediaPickerDelegate.GetOutputPath(MediaImplementation.TypeImage,
options.Directory ?? "temp",
options.Name, asset.AssetUrl?.PathExtension, index);
bool isPng = Path.GetExtension(path).ToLowerInvariant() == ".png";

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);
//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;
}

if (isPng)
{
image.AsPNG().Save(path, true);
}
else
{
image.AsJPEG().Save(path, true);
}

image?.Dispose();
image = null;
Expand Down Expand Up @@ -340,22 +348,22 @@ void GroupsEnumerator(ALAssetsGroup agroup, ref bool stop)
return;
}

//We show photos only. Let's get only them
//We show photos only. Let's get only them
agroup.SetAssetsFilter(ALAssetsFilter.AllPhotos);

//do not add empty album
if (agroup.Count == 0)
{
return;
}
//do not add empty album
if (agroup.Count == 0)
{
return;
}

//ALAssetsGroupType.All might have duplicated albums. let's skip the album if we already have it
if (assetGroups.Any(g => g.PersistentID == agroup.PersistentID))
{
return;
}

//ALAssetsGroupType.All might have duplicated albums. let's skip the album if we already have it
if (assetGroups.Any(g => g.PersistentID == agroup.PersistentID))
{
return;
}

assetGroups.Add(agroup);
assetGroups.Add(agroup);

dispatcher.BeginInvokeOnMainThread(ReloadTableView);
}
Expand Down Expand Up @@ -383,7 +391,7 @@ public override UITableViewCell GetCell(UITableView tableView, NSIndexPath index

// Get count
var g = assetGroups[indexPath.Row];

var gCount = g.Count;
cell.TextLabel.Text = string.Format("{0} ({1})", g.Name, gCount);
try
Expand All @@ -403,7 +411,7 @@ public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var assetGroup = assetGroups[indexPath.Row];
var picker = new ELCAssetTablePicker(assetGroup);

picker.LoadingTitle = LoadingTitle;
picker.PickAssetTitle = PickAssetTitle;
picker.DoneButtonTitle = DoneButtonTitle;
Expand Down Expand Up @@ -470,12 +478,14 @@ public ELCImagePickerViewController Parent
set => parent = new WeakReference(value);
}

public ELCAssetTablePicker(ALAssetsGroup assetGroup) : base(new UICollectionViewFlowLayout {
public ELCAssetTablePicker(ALAssetsGroup assetGroup) : base(new UICollectionViewFlowLayout
{
ItemSize = new CGSize(75, 75),
MinimumLineSpacing = 4,
MinimumInteritemSpacing = 4,
SectionInset = new UIEdgeInsets(0, 4, 0, 4),
ScrollDirection = UICollectionViewScrollDirection.Vertical })
ScrollDirection = UICollectionViewScrollDirection.Vertical
})
{
this.assetGroup = assetGroup;
}
Expand Down Expand Up @@ -560,7 +570,7 @@ private void AssetSelected(NSIndexPath targetIndexPath, bool selected)
asset = null;
if (mediaFile != null)
{
Parent?.SelectedMediaFiles(new List<MediaFile>{ mediaFile });
Parent?.SelectedMediaFiles(new List<MediaFile> { mediaFile });
}
else
{
Expand Down Expand Up @@ -614,10 +624,10 @@ private async void DoneClicked(object sender = null, EventArgs e = null)
var selectedMediaFiles = new MediaFile[selectedItemsCount];

//Create activity indicator if we have selected items.
//It will give the user some visual feedback that the app is still working
//if the media have to be downloaded from the iCloud
UIView pageOverlay = null;
UIActivityIndicatorView activityIndicator = null;
//It will give the user some visual feedback that the app is still working
//if the media have to be downloaded from the iCloud
UIView pageOverlay = null;
UIActivityIndicatorView activityIndicator = null;
if (selectedItemsCount > 0)
{
InvokeOnMainThread(() =>
Expand Down Expand Up @@ -654,10 +664,10 @@ private async void DoneClicked(object sender = null, EventArgs e = null)

await Task.WhenAll(tasks);

pageOverlay?.RemoveFromSuperview();
activityIndicator?.RemoveFromSuperview();
pageOverlay?.RemoveFromSuperview();
activityIndicator?.RemoveFromSuperview();

//Some items in the array might be null. Let's remove them.
//Some items in the array might be null. Let's remove them.
parent?.SelectedMediaFiles(selectedMediaFiles.Where(mf => mf != null).ToList());
}

Expand All @@ -682,7 +692,8 @@ public ALAsset Asset
public override bool Highlighted
{
get => base.Highlighted;
set {
set
{
HighlightedView.Hidden = !value;
base.Highlighted = value;
}
Expand Down
Loading

0 comments on commit b197ac8

Please sign in to comment.