Skip to content
Permalink
Browse files

(•̀o•́)ง Droid and iOS seems to be working again!

  • Loading branch information...
Cheesebaron committed Jul 13, 2015
1 parent a4b9145 commit e51168de9de6341eae4eb8d705442d940a395389
@@ -24,35 +24,6 @@ public class MvxImageView
{
private IMvxImageHelper<Bitmap> _imageHelper;

public MvxImageView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
var typedArray = context.ObtainStyledAttributes(attrs,
MvxAndroidBindingResource.Instance
.ImageViewStylableGroupId);

int numStyles = typedArray.IndexCount;
for (var i = 0; i < numStyles; ++i)
{
int attributeId = typedArray.GetIndex(i);
if (attributeId == MvxAndroidBindingResource.Instance.SourceBindId)
{
ImageUrl = typedArray.GetString(attributeId);
}
}
typedArray.Recycle();
}

public MvxImageView(Context context)
: base(context)
{
}

protected MvxImageView(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}

public string ImageUrl
{
get
@@ -97,13 +68,6 @@ public override void SetMaxWidth(int maxWidth)
base.SetMaxWidth(maxWidth);
}

[Obsolete("Use ImageUrl instead")]
public string HttpImageUrl
{
get { return ImageUrl; }
set { ImageUrl = value; }
}

protected IMvxImageHelper<Bitmap> ImageHelper
{
get
@@ -124,6 +88,31 @@ protected IMvxImageHelper<Bitmap> ImageHelper
}
}

public MvxImageView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
var typedArray = context.ObtainStyledAttributes(attrs,
MvxAndroidBindingResource.Instance
.ImageViewStylableGroupId);

int numStyles = typedArray.IndexCount;
for (var i = 0; i < numStyles; ++i)
{
int attributeId = typedArray.GetIndex(i);
if (attributeId == MvxAndroidBindingResource.Instance.SourceBindId)
{
ImageUrl = typedArray.GetString(attributeId);
}
}
typedArray.Recycle();
}

public MvxImageView(Context context)
: base(context) { }

protected MvxImageView(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }

protected override void Dispose(bool disposing)
{
if (disposing)
@@ -137,7 +126,9 @@ protected override void Dispose(bool disposing)

private void ImageHelperOnImageChanged(object sender, MvxValueEventArgs<Bitmap> mvxValueEventArgs)
{
SetImageBitmap(mvxValueEventArgs.Value);
Post(() => { // marshal back on UI Thread
SetImageBitmap(mvxValueEventArgs.Value);
});
}
}
}
@@ -15,7 +15,7 @@ public class MvxImageViewLoader
: MvxBaseImageViewLoader<UIImage>
{
public MvxImageViewLoader(Func<UIImageView> imageViewAccess, Action afterImageChangeAction = null)
: base((image) =>
: base(image =>
{
OnImage(imageViewAccess(), image);
if (afterImageChangeAction != null)
@@ -26,8 +26,12 @@ public MvxImageViewLoader(Func<UIImageView> imageViewAccess, Action afterImageCh

private static void OnImage(UIImageView imageView, UIImage image)
{
if (imageView != null)
imageView.Image = image;
if (imageView == null) return;

imageView.InvokeOnMainThread(() => {
imageView.Image = image;
});

}
}
}
@@ -21,36 +21,34 @@ public class MvxAndroidLocalFileImageLoader
: IMvxLocalFileImageLoader<Bitmap>
{
private const string ResourcePrefix = "res:";
private readonly IDictionary<CacheKey, WeakReference<Bitmap>> _memCache = new Dictionary<CacheKey, WeakReference<Bitmap>>();
private readonly IDictionary<CacheKey, WeakReference<Bitmap>> _memCache =
new Dictionary<CacheKey, WeakReference<Bitmap>>();


public Task<MvxImage<Bitmap>> Load(string localPath, bool shouldCache, int maxWidth, int maxHeight)
public async Task<MvxImage<Bitmap>> Load(string localPath, bool shouldCache, int maxWidth, int maxHeight)
{
return Task.Run(() =>
Bitmap bitmap;
var shouldAddToCache = shouldCache;
if (shouldCache && TryGetCachedBitmap(localPath, maxWidth, maxHeight, out bitmap))
{
Bitmap bitmap;
var shouldAddToCache = shouldCache;
if (shouldCache && TryGetCachedBitmap(localPath, maxWidth, maxHeight, out bitmap))
{
shouldAddToCache = false;
}
else if (localPath.StartsWith(ResourcePrefix))
{
var resourcePath = localPath.Substring(ResourcePrefix.Length);
bitmap = LoadResourceBitmap(resourcePath);
}
else
{
bitmap = LoadBitmap(localPath, maxWidth, maxHeight);
}
shouldAddToCache = false;
}
else if (localPath.StartsWith(ResourcePrefix))
{
var resourcePath = localPath.Substring(ResourcePrefix.Length);
bitmap = await LoadResourceBitmapAsync(resourcePath).ConfigureAwait(false);
}
else
{
bitmap = await LoadBitmapAsync(localPath, maxWidth, maxHeight).ConfigureAwait(false);
}

if (shouldAddToCache)
{
AddToCache(localPath, maxWidth, maxHeight, bitmap);
}
if (shouldAddToCache)
{
AddToCache(localPath, maxWidth, maxHeight, bitmap);
}

return (MvxImage<Bitmap>)new MvxAndroidImage(bitmap);
});
return (MvxImage<Bitmap>)new MvxAndroidImage(bitmap);
}

private IMvxAndroidGlobals _androidGlobals;
@@ -64,7 +62,7 @@ protected IMvxAndroidGlobals AndroidGlobals
}
}

private Bitmap LoadResourceBitmap(string resourcePath)
private async Task<Bitmap> LoadResourceBitmapAsync(string resourcePath)
{
var resources = AndroidGlobals.ApplicationContext.Resources;
var id = resources.GetIdentifier(resourcePath, "drawable", AndroidGlobals.ApplicationContext.PackageName);
@@ -75,17 +73,18 @@ private Bitmap LoadResourceBitmap(string resourcePath)
return null;
}

return BitmapFactory.DecodeResource(resources, id, new BitmapFactory.Options { InPurgeable = true });
return
await BitmapFactory.DecodeResourceAsync(resources, id,
new BitmapFactory.Options {InPurgeable = true}).ConfigureAwait(false);
}

private Bitmap LoadBitmap(string localPath, int maxWidth, int maxHeight)
private static async Task<Bitmap> LoadBitmapAsync(string localPath, int maxWidth, int maxHeight)
{
if (maxWidth > 0 || maxHeight > 0)
{
// load thumbnail - see: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
var options = new BitmapFactory.Options();
options.InJustDecodeBounds = true;
BitmapFactory.DecodeFile(localPath, options);
var options = new BitmapFactory.Options {InJustDecodeBounds = true};
await BitmapFactory.DecodeFileAsync(localPath, options).ConfigureAwait(false);

// Calculate inSampleSize
options.InSampleSize = CalculateInSampleSize(options, maxWidth, maxHeight);
@@ -94,7 +93,7 @@ private Bitmap LoadBitmap(string localPath, int maxWidth, int maxHeight)

// Decode bitmap with inSampleSize set
options.InJustDecodeBounds = false;
return BitmapFactory.DecodeFile(localPath, options);
return await BitmapFactory.DecodeFileAsync(localPath, options).ConfigureAwait(false);
}
else
{
@@ -107,7 +106,9 @@ private Bitmap LoadBitmap(string localPath, int maxWidth, int maxHeight)
// the InPurgeable option is very important for Droid memory management.
// see http://slodge.blogspot.co.uk/2013/02/huge-android-memory-bug-and-bug-hunting.html
var options = new BitmapFactory.Options { InPurgeable = true };
var image = BitmapFactory.DecodeByteArray(contents, 0, contents.Length, options);
var image = await
BitmapFactory.DecodeByteArrayAsync(contents, 0, contents.Length, options)
.ConfigureAwait(false);
return image;
}

@@ -151,29 +152,27 @@ private bool TryGetCachedBitmap(string localPath, int maxWidth, int maxHeight, o
bitmap = target;
return true;
}
else
{
_memCache.Remove(key);
}

_memCache.Remove(key);
}

bitmap = null;
return false;
}

private void AddToCache(string localPath, int maxWidth, int maxHeight, Bitmap bitmap)
{
if (bitmap != null)
{
var key = new CacheKey(localPath, maxWidth, maxHeight);
_memCache[key] = new WeakReference<Bitmap>(bitmap);
}
if (bitmap == null) return;

var key = new CacheKey(localPath, maxWidth, maxHeight);
_memCache[key] = new WeakReference<Bitmap>(bitmap);
}

private class CacheKey
{
public string LocalPath { get; set; }
public int MaxWidth { get; set; }
public int MaxHeight { get; set; }
private string LocalPath { get; set; }
private int MaxWidth { get; set; }
private int MaxHeight { get; set; }

public CacheKey(string localPath, int maxWidth, int maxHeight)
{
@@ -194,9 +193,9 @@ public override bool Equals(object obj)
if (other == null)
return false;

return object.Equals(LocalPath, other.LocalPath) &&
object.Equals(MaxWidth, other.MaxWidth) &&
object.Equals(MaxHeight, other.MaxHeight);
return Equals(LocalPath, other.LocalPath) &&
Equals(MaxWidth, other.MaxWidth) &&
Equals(MaxHeight, other.MaxHeight);
}
}
}
@@ -21,16 +21,22 @@ public class MvxTouchLocalFileImageLoader

public Task<MvxImage<UIImage>> Load(string localPath, bool shouldCache, int width, int height)
{
UIImage uiImage = null;
var tcs = new TaskCompletionSource<MvxImage<UIImage>>();

InvokeOnMainThread(() => {
UIImage uiImage;

if (localPath.StartsWith(ResourcePrefix))
uiImage = LoadResourceImage(localPath.Substring(ResourcePrefix.Length));
else
uiImage = LoadUiImage(localPath);

var result = (MvxImage<UIImage>)new MvxTouchImage(uiImage);

tcs.TrySetResult(result);
});

var result = (MvxImage<UIImage>)new MvxTouchImage(uiImage);
return Task.FromResult(result);
return tcs.Task;
}

private static UIImage LoadUiImage(string localPath)
@@ -56,7 +56,6 @@
<Compile Include="MvxHttpFileDownloader.cs" />
<Compile Include="MvxImage.cs" />
<Compile Include="MvxImageCache.cs" />
<Compile Include="MvxImageRequest.cs" />
<Compile Include="PluginLoader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

0 comments on commit e51168d

Please sign in to comment.
You can’t perform that action at this time.