C# UIImage的字节数组返回Null

C# UIImage的字节数组返回Null,c#,xamarin.ios,C#,Xamarin.ios,您好,我正在尝试从图像中获取像素值,将图像转换为灰度,最后使用NSData从字节数组返回UIImage。但它返回空值 partial void Analyze_button_TouchUpInside(UIButton sender) { int Rows = 324; //Width int Cols = 182; //Height var ImageSize = Rows * Cols; AimageByt

您好,我正在尝试从图像中获取像素值,将图像转换为灰度,最后使用
NSData
从字节数组返回
UIImage
。但它返回空值

    partial void Analyze_button_TouchUpInside(UIButton sender)
    {

        int Rows = 324; //Width
        int Cols = 182; //Height
        var ImageSize = Rows * Cols; 

        AimageBytes = new byte[ImageSize];
        RimageBytes = new byte[ImageSize];
        GimageBytes = new byte[ImageSize];
        BimageBytes = new byte[ImageSize];
        GreyImageBytes = new byte[ImageSize];

        var pixels = GetPixels(img);

        for (ind = 0; ind < ImageSize; ind++)
        {
            pval = pixels[ind];
            AimageBytes[ind] = (byte)((pval & 0xFF000000) >> 24);
            RimageBytes[ind] = (byte)((pval & 0x00FF0000) >> 16);
            GimageBytes[ind] = (byte)((pval & 0x0000FF00) >> 8);
            BimageBytes[ind] = (byte)(pval & 0x000000FF);
            GreyImageBytes[ind] = (byte)((RimageBytes[ind] + GimageBytes[ind] + BimageBytes[ind]) / 3);

        }

        var dataTest = NSData.FromArray(GreyImageBytes);
        var ImageTest = UIImage.LoadFromData(dataTest);
        PlantPhoto.Image = ImageTest;

   }
部分无效分析按钮内部触摸(UIButton发送器)
{
int Rows=324;//宽度
int Cols=182;//高度
var ImageSize=行*列;
AimageBytes=新字节[ImageSize];
RimageBytes=新字节[ImageSize];
GimageBytes=新字节[ImageSize];
BimageBytes=新字节[ImageSize];
GreyImageBytes=新字节[ImageSize];
var pixels=GetPixels(img);
对于(ind=0;ind>24);
RimageBytes[ind]=(byte)((pval&0x00FF0000)>>16);
GimageBytes[ind]=(byte)((pval&0x0000FF00)>>8);
BimageBytes[ind]=(字节)(pval&0x000000FF);
GreyImageBytes[ind]=(byte)((RimageBytes[ind]+GimageBytes[ind]+BimageBytes[ind])/3);
}
var dataTest=NSData.FromArray(GreyImageBytes);
var ImageTest=UIImage.LoadFromData(dataTest);
PlantPhoto.Image=ImageTest;
}
用于获取像素的方法:

    public static int[] GetPixels(UIImage image)
    {

        const CGBitmapFlags flags = CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast;

        var width = (int)image.CGImage.Width;
        var height = (int)image.CGImage.Height;
        var bytesPerPixel = image.CGImage.BitsPerPixel;
        var bytesPerRow = bytesPerPixel * width;
        var bitsPerComponent = image.CGImage.BitsPerComponent;
        var buffer = new byte[bytesPerRow * height];
        var pixels = new int[width * height];

        var handle = GCHandle.Alloc(buffer);
        try
        {
            using (var colorSpace = CGColorSpace.CreateGenericRgb())
            using (var context = new CGBitmapContext(buffer, width, height, bitsPerComponent, bytesPerRow, colorSpace, flags))
                context.DrawImage(new RectangleF(0, 0, width, height), image.CGImage);

            for (var y = 0; y < height; y++)
            {
                var offset = y * width;
                for (var x = 0; x < width; x++)
                {
                    var idx = bytesPerPixel * (offset + x);
                    var r = buffer[idx + 0];
                    var g = buffer[idx + 1];
                    var b = buffer[idx + 2];
                    var a = buffer[idx + 3];
                    pixels[x * y] = a << 24 | r << 16 | g << 8 | b << 0;
                }
            }
        }
        finally
        {
            handle.Free();
        }

        return pixels;
    }
publicstaticint[]GetPixels(UIImage)
{
const CGBitmapFlags flags=CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipledLast;
var width=(int)image.CGImage.width;
var height=(int)image.CGImage.height;
var bytesperpoixel=image.CGImage.BitsPerPixel;
var bytesPerRow=bytesPerPixel*宽度;
var bitsPerComponent=image.CGImage.bitsPerComponent;
var buffer=新字节[bytesPerRow*高度];
var像素=新整数[宽度*高度];
var handle=GCHandle.Alloc(缓冲区);
尝试
{
使用(var colorSpace=CGColorSpace.CreateGenericRgb())
使用(var-context=new-cgitmapcontext(缓冲区、宽度、高度、bitsPerComponent、bytesPerRow、颜色空间、标志))
DrawImage(新矩形f(0,0,宽度,高度),image.CGImage);
对于(变量y=0;ypixels[x*y]=a我正在尝试找出数据格式是否与匹配uiimage数据参数的格式不同。不幸的是,我不得不使用我发布的方法。
尝试将图像转换为字节数组并返回,结果正常

NSData imageDataTest = img.AsPNG();

                Byte[] testByteArray = new Byte[imageDataTest.Length];
                Marshal.Copy(imageDataTest.Bytes, testByteArray, 0, Convert.ToInt32(imageDataTest.Length));

UIImage backToImage = UIImage.LoadFromData(imageDataTest);

似乎testByteArrayGreyImageBytes在字节数组中具有相同的结构。

我试图找出数据格式是否与匹配uiimage数据参数的格式不同。不幸的是,我不得不使用我发布的方法。 尝试将图像转换为字节数组并返回,结果正常

NSData imageDataTest = img.AsPNG();

                Byte[] testByteArray = new Byte[imageDataTest.Length];
                Marshal.Copy(imageDataTest.Bytes, testByteArray, 0, Convert.ToInt32(imageDataTest.Length));

UIImage backToImage = UIImage.LoadFromData(imageDataTest);

似乎testByteArrayGreyImageBytes都具有相同的字节数组结构。

来自initWithData上的Apple文档:“数据参数中的数据格式必须与系统支持的图像类型之一的文件格式相匹配。”您必须使用此方法使图像灰度化吗?在xamarin ios中,图像可以直接转换为灰度。从initWithData上的Apple文档:“数据参数中的数据的格式必须与系统支持的图像类型之一的文件格式相匹配。”您必须使用此方法使图像灰度化吗?在xamarin ios中,图像可以直接转换为灰度。