Objective c 如何在窗口上显示像素阵列?

Objective c 如何在窗口上显示像素阵列?,objective-c,cocoa,xcode4,pixel,cgimage,Objective C,Cocoa,Xcode4,Pixel,Cgimage,非常简单的问题。。。我有一个像素阵列,如何在屏幕上显示它们 #define WIDTH 10 #define HEIGHT 10 #define SIZE WIDTH*HEIGHT unsigned short pixels[SIZE]; for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { pixels[j*HEIGHT + i] = 0xFFFF; } } #

非常简单的问题。。。我有一个像素阵列,如何在屏幕上显示它们

#define WIDTH 10
#define HEIGHT 10
#define SIZE WIDTH*HEIGHT

unsigned short pixels[SIZE];

for (int i = 0; i < WIDTH; i++) {
    for (int j = 0; j < HEIGHT; j++) {
        pixels[j*HEIGHT + i] = 0xFFFF;
    }
}
#定义宽度10
#定义高度10
#定义尺寸宽度*高度
无符号短像素[大小];
对于(int i=0;i

就这样。。。现在我如何在屏幕上显示它们呢?

有很多方法可以做到这一点。更简单的方法之一是使用
CGContextDrawImage
。在drawRect中:

#define WIDTH 10
#define HEIGHT 10
#define SIZE WIDTH*HEIGHT

unsigned short pixels[SIZE];

for (int i = 0; i < WIDTH; i++) {
    for (int j = 0; j < HEIGHT; j++) {
        pixels[j*HEIGHT + i] = 0xFFFF;
    }
}
CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
CGDataProviderRef provider = CGDataProviderCreateWithData(nil, bitmap, bitmap_bytes, nil);
CGImageRef img = CGImageCreate(..., provider, ...);
CGDataProviderRelease(provider);    
CGContextDrawImage(ctx, dstRect, img);
CGImageRelease(img);
CGImageCreate有一系列参数,我在这里遗漏了这些参数,因为正确的值取决于位图格式


请注意,如果位图是静态的,那么保留
cgimagerf
而不是立即处理它可能是有意义的。您最了解应用程序的工作方式,因此您可以决定这是否合理。

有很多方法可以做到这一点。更简单的方法之一是使用
CGContextDrawImage
。在drawRect中:

CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
CGDataProviderRef provider = CGDataProviderCreateWithData(nil, bitmap, bitmap_bytes, nil);
CGImageRef img = CGImageCreate(..., provider, ...);
CGDataProviderRelease(provider);    
CGContextDrawImage(ctx, dstRect, img);
CGImageRelease(img);
CGImageCreate有一系列参数,我在这里遗漏了这些参数,因为正确的值取决于位图格式


请注意,如果位图是静态的,那么保留
cgimagerf
而不是立即处理它可能是有意义的。您最了解应用程序的工作方式,因此您可以决定这是否合理。

我通过使用NSBitmapImageRep的NSImageView从像素值创建图像来解决此问题。有很多选项可以选择如何创建像素值。在我的例子中,我使用了32位像素(RGBA)。在此代码中,
像素
是像素值的巨大数组<代码>显示
是NSImageView的出口

    NSBitmapImageRep *myBitmap;
    NSImage *myImage;
    unsigned char *buff[4];
    unsigned char *pixels;
    int width, height, rectSize;
    NSRect myBounds;

    myBounds = [display bounds];
    width = myBounds.size.width;
    height = myBounds.size.height;

    rectSize = width * height;


    memset(buff, 0, sizeof(buff));
    pixels = malloc(rectSize * 4);

    (fill in pixels array)

    buff[0] = pixels;

    myBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:buff
            pixelsWide:width
            pixelsHigh:height 
            bitsPerSample:8
            samplesPerPixel:4 
            hasAlpha:YES
            isPlanar:NO
            colorSpaceName:NSCalibratedRGBColorSpace
            bitmapFormat:0
            bytesPerRow:(4 * width)
            bitsPerPixel:32];

    myImage = [[NSImage alloc] init];
    [myImage addRepresentation:myBitmap];
    [display setImage: myImage];
    [myImage release];
    [myBitmap release];

    free(pixels);

我通过使用NSBitmapImageRep的NSImageView从像素值创建图像来解决这个问题。有很多选项可以选择如何创建像素值。在我的例子中,我使用了32位像素(RGBA)。在此代码中,
像素
是像素值的巨大数组<代码>显示是NSImageView的出口

    NSBitmapImageRep *myBitmap;
    NSImage *myImage;
    unsigned char *buff[4];
    unsigned char *pixels;
    int width, height, rectSize;
    NSRect myBounds;

    myBounds = [display bounds];
    width = myBounds.size.width;
    height = myBounds.size.height;

    rectSize = width * height;


    memset(buff, 0, sizeof(buff));
    pixels = malloc(rectSize * 4);

    (fill in pixels array)

    buff[0] = pixels;

    myBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:buff
            pixelsWide:width
            pixelsHigh:height 
            bitsPerSample:8
            samplesPerPixel:4 
            hasAlpha:YES
            isPlanar:NO
            colorSpaceName:NSCalibratedRGBColorSpace
            bitmapFormat:0
            bytesPerRow:(4 * width)
            bitsPerPixel:32];

    myImage = [[NSImage alloc] init];
    [myImage addRepresentation:myBitmap];
    [display setImage: myImage];
    [myImage release];
    [myBitmap release];

    free(pixels);
  • 创建一个新的“Cocoa应用程序”(如果您不知道如何创建Cocoa应用程序,请转到)
  • 子类NSView(如果您不知道如何子类化视图)
  • 在interface builder上将NSWindow的大小设置为400x400
  • 在NSView中使用此代码

    #import "MyView.h"
    
    @implementation MyView
    
    #define WIDTH 400
    #define HEIGHT 400
    #define SIZE (WIDTH*HEIGHT)
    #define BYTES_PER_PIXEL 2
    #define BITS_PER_COMPONENT 5
    #define BITS_PER_PIXEL 16
    
    - (id)initWithFrame:(NSRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code here.
        }
        return self;
    }
    
    - (void)drawRect:(NSRect)dirtyRect
    {
        // Get current context
        CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    
        // Colorspace RGB
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        // Pixel Matrix allocation
        unsigned short *pixels = calloc(SIZE, sizeof(unsigned short));
    
        // Random pixels will give you a non-organized RAINBOW 
        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                pixels[i+ j*HEIGHT] = arc4random() % USHRT_MAX;
            }
        }
    
        // Provider
        CGDataProviderRef provider = CGDataProviderCreateWithData(nil, pixels, SIZE, nil);
    
        // CGImage
        CGImageRef image = CGImageCreate(WIDTH,
                                         HEIGHT,
                                         BITS_PER_COMPONENT,
                                         BITS_PER_PIXEL,
                                         BYTES_PER_PIXEL*WIDTH,
                                         colorSpace,
                                         kCGImageAlphaNoneSkipFirst,
                                         // xRRRRRGGGGGBBBBB - 16-bits, first bit is ignored!
                                         provider,
                                         nil, //No decode
                                         NO,  //No interpolation
                                         kCGRenderingIntentDefault); // Default rendering
    
    // Draw
    CGContextDrawImage(context, self.bounds, image);
    
    // Once everything is written on screen we can release everything
    CGImageRelease(image);
    CGColorSpaceRelease(colorSpace);
    CGDataProviderRelease(provider);
    
    }
    @end
    
    #导入“MyView.h”
    @实现MyView
    #定义宽度400
    #定义高度400
    #定义大小(宽度*高度)
    #定义每像素2的字节数
    #定义每个组件5的位
    #定义每像素16的位
    -(id)initWithFrame:(NSRect)帧
    {
    self=[super initWithFrame:frame];
    如果(自我){
    //这里是初始化代码。
    }
    回归自我;
    }
    -(void)drawRect:(NSRect)dirtyRect
    {
    //获取当前上下文
    CGContextRef上下文=(CGContextRef)[[NSGraphicsContext currentContext]GraphicsSport];
    //色彩空间RGB
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    //像素矩阵分配
    无符号短*像素=calloc(大小,sizeof(无符号短));
    //随机像素会给你一个无组织的彩虹
    对于(int i=0;i
  • 创建一个新的“Cocoa应用程序”(如果您不知道如何创建Cocoa应用程序,请转到)
  • 子类NSView(如果您不知道如何子类化视图)
  • 在interface builder上将NSWindow的大小设置为400x400
  • 在NSView中使用此代码

    #import "MyView.h"
    
    @implementation MyView
    
    #define WIDTH 400
    #define HEIGHT 400
    #define SIZE (WIDTH*HEIGHT)
    #define BYTES_PER_PIXEL 2
    #define BITS_PER_COMPONENT 5
    #define BITS_PER_PIXEL 16
    
    - (id)initWithFrame:(NSRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code here.
        }
        return self;
    }
    
    - (void)drawRect:(NSRect)dirtyRect
    {
        // Get current context
        CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    
        // Colorspace RGB
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        // Pixel Matrix allocation
        unsigned short *pixels = calloc(SIZE, sizeof(unsigned short));
    
        // Random pixels will give you a non-organized RAINBOW 
        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                pixels[i+ j*HEIGHT] = arc4random() % USHRT_MAX;
            }
        }
    
        // Provider
        CGDataProviderRef provider = CGDataProviderCreateWithData(nil, pixels, SIZE, nil);
    
        // CGImage
        CGImageRef image = CGImageCreate(WIDTH,
                                         HEIGHT,
                                         BITS_PER_COMPONENT,
                                         BITS_PER_PIXEL,
                                         BYTES_PER_PIXEL*WIDTH,
                                         colorSpace,
                                         kCGImageAlphaNoneSkipFirst,
                                         // xRRRRRGGGGGBBBBB - 16-bits, first bit is ignored!
                                         provider,
                                         nil, //No decode
                                         NO,  //No interpolation
                                         kCGRenderingIntentDefault); // Default rendering
    
    // Draw
    CGContextDrawImage(context, self.bounds, image);
    
    // Once everything is written on screen we can release everything
    CGImageRelease(image);
    CGColorSpaceRelease(colorSpace);
    CGDataProviderRelease(provider);
    
    }
    @end
    
    #导入“MyView.h”
    @实现MyView
    #定义宽度400
    #定义高度400
    #定义大小(宽度*高度)
    #定义每像素2的字节数
    #定义每个组件5的位
    #定义每像素16的位
    -(id)initWithFrame:(NSRect)帧
    {
    self=[super initWithFrame:frame];
    如果(自我){
    //这里是初始化代码。
    }
    回归自我;
    }
    -(void)drawRect:(NSRect)dirtyRect
    {
    //获取当前上下文
    CGContextRef上下文=(CGContextRef)[[NSGraphicsContext currentContext]GraphicsSport];
    //色彩空间RGB
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    //像素矩阵分配
    无符号短*像素=calloc(大小,sizeof(无符号短));
    //随机像素会给你一个无组织的彩虹
    对于(int i=0;i