本文介绍了检索UIImage的像素alpha值(MonoTouch)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此问题与 1042830 重复,但是特定于MonoTouch.有没有一种方法比分配IntPtr,使用CGBitmapContext插入IntPtr然后在适当的偏移量处读取字节更安全?
This question is a duplicate of 1042830, but MonoTouch-specific. Is there a way that's safer than allocating an IntPtr, drawing into it using CGBitmapContext and then reading bytes at the appropriate offset?
推荐答案
我不知道是否可以回答您自己的问题,但是:
I don't know if it's kosher to answer your own question, but:
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;
}
//Note: Unsafe code
unsafe byte GetByte(int offset, IntPtr buffer)
{
byte* bufferAsBytes = (byte*) buffer;
return bufferAsBytes[offset];
}
//Example of using it...
public override bool PointInside (PointF point, UIEvent uievent)
{
//Lazy initialize
if(bitmapData == IntPtr.Zero)
{
bitmapData = RequestImagePixelData(Image);
}
//Check for out of bounds
if(point.Y < 0 || point.X < 0 || point.Y > Image.Size.Height || point.X > Image.Size.Width)
{
return false;
}
var startByte = (int) ((point.Y * this.Image.Size.Width + point.X) * 4);
byte alpha = GetByte(startByte, this.bitmapData);
Console.WriteLine("Alpha value of {0}, {1} is {2}", point.X, point.Y, alpha);
...etc...
这篇关于检索UIImage的像素alpha值(MonoTouch)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!