1000帧只是为了方便表述,实际情况是这样:
我从摄像头读取视频数据转换为UIImage(iOS平台),再将UIImage转为CCSprite,并显示出来,
然后每隔0.1秒移除上一个CCSprite,重新从UIImage创建一个CCSprite并显示出来。
我用Sprite->removeFromParentAndCleanup(true) 来移除精灵,但是遇到了问题,内存飙升很快,程序在执行几秒后就奔溃了。
(这里的Sprite是实时产生,动画序列可以看作无限长)
请问各位大大是否有解决这个问题的办法或者思路?万分感谢!
现在步骤是:UIImage 生成 Texture2d 生成 Sprite ,每帧移除上一个sprite,新创建一个sprite加入场景,
问题还是没解决,运行不到十秒就崩溃
这是UIImage转Sprite方法:
cocos2d::Sprite *getSpriteFromUIImage(UIImage *img)
{
// First get the image into your data buffer
CGImageRef imageRef =
![](;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char rawData = (unsigned char) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
/* create a texture with the raw data, and use that to create a CCSprite*/
Texture2D *tempTexture = new Texture2D();
tempTexture->initWithData(rawData,
4 * width * height,
Texture2D::PixelFormat::RGBA8888,
width, height,
cocos2d::Size(width, height));
tempTexture->autorelease();
cocos2d::Sprite *finalSprite = cocos2d::Sprite::createWithTexture(tempTexture);
free(rawData);
return finalSprite;
}
加上
tempTexture->autorelease();不崩了,低级错误。
但是这种方式效率和视频质量都有问题。
)