我觉得这很有趣
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;
}
}
}