代码之家  ›  专栏  ›  技术社区  ›  ediheld

在iOS中,我可以使用什么来代替NSBitmapImageRep?(水波纹示例)

  •  2
  • ediheld  · 技术社区  · 10 年前

    我觉得这很有趣 post about water ripple simulation 。它包括 Xcode project 有一个OSX的小可可应用程序。我尝试使用Swift在iOS上运行应用程序,但无法找到解决以下问题的方法:

    OSX应用程序使用 NSBitmapImageRep 将位图数据从数组绘制到屏幕上(这发生在BBRippleView.m中)。不幸地 NSBitmapImageRep 在iOS中不可用。我尝试使用 CGImage 相反,这是“有点管用”的:我可以看到一些水波,但它们以某种奇怪的方式分裂,它们不会按照预期的方式移动。我猜CGImage希望以与当前不同的格式获取位图数据。

    编辑:这是我的iOS RippleView类: RippleView.swift

    编辑2:已修改 RippleView.swift

    使用Swift在iOS中实现以下OSX代码段的正确方法是什么?

            image = [[NSImage alloc] initWithSize:NSMakeSize(cols, rows)];
            bufrep = [[NSBitmapImageRep alloc]
                                initWithBitmapDataPlanes:NULL
                                pixelsWide:cols
                                pixelsHigh:rows
                                bitsPerSample:8
                                samplesPerPixel:1
                                hasAlpha:NO
                                isPlanar:YES
                                colorSpaceName:NSDeviceWhiteColorSpace
                                bytesPerRow:0
                                bitsPerPixel:0];
            [image addRepresentation:bufrep];
    

    -(void)applyBuffer
    {
        unsigned char* data = [bufrep bitmapData];
        int x = 0;
        int y = 0;
        int position = 0;
        for ( y = 0; y < rows; y ++) {
            for ( x = 0; x < cols ; x ++) {
                position = (y * cols) + x;
                data[position] = (char)buffer2[position] << 1;
            }           
        }       
    }
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   jcaron    10 年前

    你在说 CGImageCreate 你的图像是灰度的,每像素8位。但您的缓冲区是 Int s、 其为32或64位(4或8字节)。

    切换到 UInt8 ,这应该会有很大帮助。