uikit - 检索 UIImage 的像素 alpha 值 (MonoTouch)

标签 uikit uiimage xamarin.ios pixel unsafe

此问题与1042830重复。 ,但是 MonoTouch 特定的。有没有一种方法比分配 IntPtr、使用 CGBitmapContext 绘制到其中然后在适当的偏移处读取字节更安全?

最佳答案

我不知道回答你自己的问题是否合理,但是:

    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...

关于uikit - 检索 UIImage 的像素 alpha 值 (MonoTouch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597198/

相关文章:

ios - 锁定 UIView 相对于 UIScrollView 的位置

ios - 将自定义 UIImage 设置为 UIImageRenderingModeAlwaysTemplate 的 UISlider

objective-c - 如何缩放屏幕图像以便在 Facebook 墙上发布?

ios - 如何将 URL 中的图像加载到 UIImage 数组中并使用 Swift 在 UIScrollView 中使用它们

iOS/MonoTouch : Become first responder

ios - 当 UILabel 的高度增长时, View 不会移动

ios - 如何从 Core Graphics 生成动态明暗模式 UIImage?

xamarin.ios - Xamarin iOS 绑定(bind) - 协议(protocol) - 无法创建抽象类的实例

iOS:使用 anchor 以编程方式将 UIImageView 与底部对齐

c# - 更改导航栏标题的颜色