I’m a bit new to MonoTouch / Mono for Android. I work on a platform for iOS and Android app, and I’m in the process to port our native code to C#.
Our app displays images. In Mono however there is no common image type. In Mono for Android an option is using Bitmap (cannot be subclassed) or BitmapDrawable, in iOS there’s UIImage.
To make some code in the app more consistent across platforms it seems to me a good idea to create a common class for dealing with images. The class is basically a facade for the corresponding “native” classes. One of the functionalities I want this class to have, is to create an image object from an array of bytes. Currently my code looks like the following (and works fine):
using System;
#if ANDROID
using Android.Graphics;
using Android.Graphics.Drawables;
#else
using MonoTouch.UIKit;
using MonoTouch.Foundation;
#endif
namespace OurCompany.Core
{
#if ANDROID
public class Image : BitmapDrawable
{
public Image (byte[] bytes) : base(BitmapFactory.DecodeByteArray (bytes, 0, bytes.Length))
{
}
}
#else
public class Image : UIImage
{
public Image (byte[] bytes) : base(NSData.FromArray(bytes), 1)
{
}
}
#endif
}
Now I wonder, are there any problems I might encounter using this setup? I would like to know if I might expect any performance, caching or other issues with a setup like this.
Yes it makes sense.
Although personally I prefer the approach of using an interface instead of using #if – I find interfaces work better with refactoring, testing, etc – and that the approach scales better as you port to new platforms (mac, winrt, wp, etc)
As long as you design your interface carefully then you shouldn’t hit any specific performance problems – but if ever you find your code starting to work at the pixel array level, then you might be missing out on native optimisation opportunities.
For some ideas in a similar area -drawing – see Frank’s work on https://github.com/praeclarum/CrossGraphics