使用CGBitmapContext生成错误的图像

时间:2015-10-27 14:21:00

标签: xamarin cgbitmapcontextcreate cgbitmapcontext

我有这段代码

private static UIImage Copy(UIImage self) {
        var inImage = self.CGImage;
        var width = inImage.Width;
        var height = inImage.Height;
        var bits = inImage.BitsPerComponent;
        var bitsPerRow = inImage.BytesPerRow;
        var colorSpace = inImage.ColorSpace;
        var alphaInfo = inImage.AlphaInfo;

        using (var jpeg = UIImage.FromImage (inImage).AsJPEG ()) {
            var bytes = new Byte[jpeg.Length];
            System.Runtime.InteropServices.Marshal.Copy (jpeg.Bytes, bytes, 0, Convert.ToInt32 (jpeg.Length));

            using (CGBitmapContext ctx = new CGBitmapContext (bytes, width, height, bits, bitsPerRow, colorSpace, alphaInfo)) {
                return UIImage.FromImage (ctx.ToImage ());
            }
        }
    }

我只是从原始图像中获取字节数组并使用CGBitmapContext创建图像,但结果是:

enter image description here

enter image description here

0 个答案:

没有答案