Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 unsafe byte GetByte(int offset, IntPtr buffer)
{
byte* bufferAsBytes = (byte*) buffer;
return bufferAsBytes[offset];
}

//Listing 2-7
protected CGBitmapContext CreateARGBBitmapContext(CGImage inImage)
{
var pixelsWide = inImage.Width;
var pixelsHigh = inImage.Height;
var bitmapBytesPerRow = pixelsWide * 4;
var bitmapByteCount = bitmapBytesPerRow * pixelsHigh;
//Note implicit colorSpace.Dispose()
using(var colorSpace = CGColorSpace.CreateDeviceRGB())
{
//Allocate the bitmap and create context
var bitmapData = Marshal.AllocHGlobal(bitmapByteCount);
//I think this is unnecessary, as I believe Marshal.AllocHGlobal will throw OutOfMemoryException
if(bitmapData == IntPtr.Zero)
{
throw new Exception("Memory not allocated.");
}

var context = new CGBitmapContext(bitmapData, pixelsWide, pixelsHigh, 8,
bitmapBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst);
if(context == null)
{
throw new Exception("Context not created");
}
return context;
}
}

//Store pixel data as an ARGB Bitmap
protected IntPtr RequestImagePixelData(UIImage inImage)
{
imageSize = inImage.Size;
CGBitmapContext ctxt = CreateARGBBitmapContext(inImage.CGImage);
var rect = new RectangleF(0.0f, 0.0f, imageSize.Width, imageSize.Height);
ctxt.DrawImage(rect, inImage.CGImage);
var data = ctxt.Data;
return data;

}