iPhone App - 在屏幕上显示缓冲区中的像素数据 [英] iPhone App - Display pixel data present in buffer on screen

查看:143
本文介绍了iPhone App - 在屏幕上显示缓冲区中的像素数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C编写的视频解码器应用程序的源代码,我现在正在移植到iphone上。

I have the source code for a video decoder application written in C, which I'm now porting on iphone.

我的问题如下:

我有一个缓冲区中的帧的RGBA像素数据,我需要在屏幕上显示。我的缓冲区是unsigned char类型。 (我无法将其更改为任何其他数据类型,因为源代码太大而且不是由我编写的。)

I have RGBA pixel data for a frame in a buffer that I need to display on the screen. My buffer is of type unsigned char. (I cannot change it to any other data type as the source code is too huge and not written by me.)

我在网上找到的大多数链接都是关于如何在屏幕上绘制和显示像素或如何显示数组中的像素,但没有人说如何显示缓冲区中存在的像素数据。

Most of the links I found on the net say about how to "draw and display pixels" on the screen or how to "display pixels present in an array", but none of then say how to "display pixel data present in a buffer".

我打算使用石英2D。我需要做的只是在屏幕上显示缓冲区内容。没有修改!虽然我的问题听起来很简单,但我找不到任何API可以做同样的事情。我找不到任何足够有用的链接或文档。

I'm planning to use quartz 2D. All I need to do is just display the buffer contents on the screen. No modifications! Although my problem sounds very simple, there isn't any API that I could find to do the same. I couldn't find any appropriate link or document that was useful enough.

请帮忙!
提前致谢。

Kindly help! Thanks in advance.

推荐答案

您可以使用 CGContext 从原始像素数据创建 CGImage 的数据结构。我已经快速写了一个基本的例子:

You can use the CGContext data structure to create a CGImage from raw pixel data. I've quickly written a basic example:

- (CGImageRef)drawBufferWidth:(size_t)width height:(size_t)height pixels:(void *)pixels
{
    unsigned char (*buf)[width][4] = pixels;


    static CGColorSpaceRef csp = NULL;
    if (!csp) {
        csp = CGColorSpaceCreateDeviceRGB();
    }

    CGContextRef ctx = CGBitmapContextCreate(
        buf,
        width,
        height,
        8, // 8 bits per pixel component
        width * 4, // 4 bytes per row
        csp,
        kCGImageAlphaPremultipliedLast
    );

    CGImageRef img = CGBitmapContextCreateImage(ctx);
    CGContextRelease(ctx);
    return img;
}

您可以像这样调用此方法(我使用了视图控制器) :

You can call this method like this (I've used a view controller):

- (void)viewDidLoad
{
    [super viewDidLoad];

    const size_t width = 320;
    const size_t height = 460;

    unsigned char (*buf)[width][4] = malloc(sizeof(*buf) * height);

    // fill up `buf` here
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            buf[y][x][0] = x * 255 / width;
            buf[y][x][1] = y * 255 / height;
            buf[y][x][2] =   0;
            buf[y][x][3] = 255;
        }
    }

    CGImageRef img = [self drawBufferWidth:320 height:460 pixels:buf];
    self.imageView.image = [UIImage imageWithCGImage:img];
    CGImageRelease(img);
}

这篇关于iPhone App - 在屏幕上显示缓冲区中的像素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆