cocos2dx3.X uiimage 转 texture2d 或者 image 失败
2017-07-13 22:20
363 查看
今天试了网络上各种流行的转法,最终结果都是失败告终。
在void* data类型转到unsighe char* data过程中报错,直接报空指针。
类似失败代码1:
// uiimage
转
texture2d等的都会出现错误
错误原因
未知
cocos2d::Texture2D
*texture =
new cocos2d::Texture2D();
CGImageRef
imageRef = [(UIImage*)pImage
CGImage];
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);
if
(texture->initWithData(rawData, height * width *
4,
cocos2d::Texture2D::PixelFormat::RGBA8888,
(int)width, (int)height,
cocos2d::Size(width, height)))
{
if
(ROLE_MANAGER->saveImageForFileName(texture))
{
CCLOG(" image success to save !~");
texture->autorelease();
flag = true;
}
else{
CCLOG(" image fail to save !~");
}
}
else{
CCLOG(" uiimage transfrom is fail!~ init error. ");
}
if
(!flag)
delete texture;
类似代码2:
cocos2d::Image
*image =
new(std::nothrow)
cocos2d::Image();
cocos2d::Texture2D
*texture =
new cocos2d::Texture2D();
// if (image->initWithRawData((unsigned char*)pData, pLength, pWidth, pHeight, 4)) {
// if (ROLE_MANAGER->saveImageForFileName(image)) {
if
(texture->initWithData(pData, pLength,
cocos2d::Texture2D::PixelFormat::RGBA8888,
pWidth, pHeight, cocos2d::Size(pWidth,
pHeight))) {
if
(ROLE_MANAGER->saveImageForFileName(texture))
{
CCLOG(" image success to save !~");
image->autorelease();
flag = true;
}
else{
CCLOG(" image fail to save !~");
}
}
else{
CCLOG(" uiimage transfrom is fail!~ init error. ");
}
在void* data类型转到unsighe char* data过程中报错,直接报空指针。
类似失败代码1:
// uiimage
转
texture2d等的都会出现错误
错误原因
未知
cocos2d::Texture2D
*texture =
new cocos2d::Texture2D();
CGImageRef
imageRef = [(UIImage*)pImage
CGImage];
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);
if
(texture->initWithData(rawData, height * width *
4,
cocos2d::Texture2D::PixelFormat::RGBA8888,
(int)width, (int)height,
cocos2d::Size(width, height)))
{
if
(ROLE_MANAGER->saveImageForFileName(texture))
{
CCLOG(" image success to save !~");
texture->autorelease();
flag = true;
}
else{
CCLOG(" image fail to save !~");
}
}
else{
CCLOG(" uiimage transfrom is fail!~ init error. ");
}
if
(!flag)
delete texture;
类似代码2:
cocos2d::Image
*image =
new(std::nothrow)
cocos2d::Image();
cocos2d::Texture2D
*texture =
new cocos2d::Texture2D();
// if (image->initWithRawData((unsigned char*)pData, pLength, pWidth, pHeight, 4)) {
// if (ROLE_MANAGER->saveImageForFileName(image)) {
if
(texture->initWithData(pData, pLength,
cocos2d::Texture2D::PixelFormat::RGBA8888,
pWidth, pHeight, cocos2d::Size(pWidth,
pHeight))) {
if
(ROLE_MANAGER->saveImageForFileName(texture))
{
CCLOG(" image success to save !~");
image->autorelease();
flag = true;
}
else{
CCLOG(" image fail to save !~");
}
}
else{
CCLOG(" uiimage transfrom is fail!~ init error. ");
}
相关文章推荐
- 使用IP连接SQL SERVER或者配置为连接字符串失败
- iOS的圆形或者圆角ImageView
- SDWebImage访问HTTPS站点获取图片资源失败解决办法
- release版本成功,debug版本失败,或者debug 版本成功,release版本失败
- 解决卸载或者安装程序失败的问题,提示:错误 1402。无法打开键UNKNOWN\Components\xxx
- cocos2dx游戏开发之利用多点触摸(实现缩放功能或者简单的手势识别)
- 编译cocos2dx工程,生成libcocos2dcpp.so文件失败
- 阿里云迁移或者欠费重启后访问失败
- Hibernate 插入(更新)不了数据(或者插入(更新)失败)
- arcmap导出或者打印时插入的图片和对象绘制失败
- cocos2dx-js RenderTexture 将多张图片合并成一张图片
- opengl glBindTexture为何失败
- Genymotion 下载模拟器失败或者慢
- unity运行时www下载的texture图片转换成sprite赋值到image上
- Unity_二维纹理 Texture 2D_2_高级纹理
- cocos2dx在vs2013加载项目失败(针对路径更改问题)
- MySQL安装失败或者重装后安装失败解决方法
- 解决“启动程序失败,路径或者权限错误”的执行问题
- xilinx 安装驱动失败或者驱动不适用于该平台解决方案
- android实现正方形的ImageView、Layout等(即高度适应宽度或者宽度适应高度)