CAImage、CAImageView(显示一张图片)

2018-09-08 15:51 更新

类说明

CAImage贴图类,即内存中的纹理,主要用于创建贴图对象,将图像加载进内存,是所有有关贴图类的基础


CAImage 属性(点击方法名可查看方法介绍)

属性说明
PixelFormat像素格式
PixelsWide像素宽
PixelsHigh像素高
ContentSize内容大小
ShaderProgram着色程序
Monochrome单色
Data数据
DataLenght数据长度


CAImage 方法(点击方法名可查看方法介绍)

方法说明
create创建,默认Frame为(0,0,0,0)
createWithImageDataNoCache创建,默认没有缓存
createWithImageData创建,默认有缓存
createWithString创建,并指定文本内容
getFontHeight获得字体高度
getStringWidth获得字体宽度
cutStringByWidth减少字符串的宽度
getStringHeight减少字符串的高度
scaleToNewImageWithImage缩放当前图像做新图像
scaleToNewImageWithImage缩放当前图像做新图像
generateMipmapsWithImage生成mipmap图像
createWithRawDataNoCache创建,并指定Raw Data,默认没有缓存
createWithRawData创建,并指定Raw Data,默认没有缓存
initWithImageFile初始化,并指定其图像文件
initWithImageFileThreadSafe初始化,并指定其图像文件(支持子线程)
initWithImageData初始化,并指定其图像缓存
initWithRawData初始化,并指定其原数据
description添加图像描述
releaseData释放所有数据
releaseData释放指定数据
drawAtPoint基于某个点显示(大小为图像大小)
drawInRect基于某个矩形区域显示
initWithETCFile初始化,并指定其ETC文件
stringForFormat格式字符串
saveToFile保存到文件
getImageFileType获取图像文件类型
getAspectRatio得到长宽比
detectFormat检测格式
premultipliedImageData进行图像数据
repremultipliedImageData再进行图像数据
updateGifImageWithIndex通过index更新GIF图像
getGifImageIndex获取GIF图像的index
getGifImageCounts获取GIF图像的参数
reloadAllImages重新加载所有图像


CAImageView 属性(点击方法名可查看方法介绍)

属性说明
ImageViewScaleType图像缩放类型
AnimationImages动画图像
AnimationDuration动画间隔的时间
AnimationRepeatCount动画重复次数


CAImageView 方法(点击方法名可查看方法介绍)

方法说明
create创建,默认Frame为(0,0,0,0)
createWithImage创建,并指定Image
createWithFrame创建,并指定Frame,默认Frame为(0,0,0,0)
createWithCenter创建,并指定Center,默认Center为(0,0,0,0)
init初始化
initWithImage初始化,并指定其图像
setImage设置图像
getImage获取图像
setImageAsyncWithFile异步设置图像文件
startAnimating开始动画
stopAnimating结束动画
isAnimating查看图像动画


使用方法


//创建一个CAImage
    CAImage* image = CAImage::create("HelloWorld.png");
 
//CAImage函数
//打印贴图描述
    CCLog("Description:%s", image->description());
 
//打印像素格式
    CCLog("StringForFormat:%s", image->stringForFormat());
     
//打印像素位深
    CCLog("bitsPerPixelForFormat:%d", image->bitsPerPixelForFormat());
     
//方法一:通过CAImage创建一个CAImageView
    CAImageView* imageView = CAImageView::createWithImage(image);
     
//设置显示的范围(如果不设置,默认不会被显示出来)
    imageView->setFrame(winRect);
     
//添加到绘制
     this->getView()->addSubview(imageView);
      
//方法二:通过设置Center创建一个CAImageViewD
     CAImageView* imageView_1 = CAImageView::createWithCenter(DRect(50,50,100,100));
      
//设置显示的纹理
     imageView_1->setImage(image);
      
//异步加载
     //imageView_1->setImageAsyncWithFile("source_material/stepper_inc_n.png");
      
//添加到绘制,设置z轴为1
     this->getView()->insertSubview(imageView_1,1);

        CCLog的打印结果:

    Description:<CAImage | Name = 3 | Dimensions = 640 x 960 | Coordinates = (1.00, 1.00)>
    StringForFormat:RGB888
    bitsPerPixelForFormat:32

CAImage 属性说明

PixelFormat

类型:CAImage::PixelFormat

解释:像素格式。get{}。


PixelsWide

类型:unsigned int

解释:像素宽。get{}。


PixelsHigh

类型:unsigned int

解释:像素高。get{}。


ContentSize

类型:DSize

解释:内容大小。get{}。


ShaderProgram

类型:CAGLProgram*

解释:着色程序。set/get{}。


Monochrome

类型:bool

解释:单色。is{}。


Data

类型:unsigned char*

解释:数据。get{}。


DataLenght

类型:unsigned long

解释:数据长度。get{}。


CAImage 方法说明

static CAImage* create(const std::string& file);

返回值:static CAImage*

参数:

类型参数名说明
const std::string&file图像名

解释:创建,默认Frame为(0,0,0,0)


static CAImage* createWithImageDataNoCache(const unsigned char * data, unsigned long lenght);

返回值:static CAImage*

参数:

类型参数名说明
const unsigned char *data数据
 unsigned longlenght长度

解释:创建,默认没有缓存


static CAImage* createWithImageData(const unsigned char * data, unsigned long lenght, const std::string& key);

返回值:static CAImage*

参数:

类型参数名说明
const unsigned char *data数据
unsigned longlenght长度
const std::string&key属性

解释:创建,默认有缓存


static CAImage* createWithString(const char *text, const char *fontName, float fontSize, const DSize& dimensions, CATextAlignment hAlignment, CAVerticalTextAlignment vAlignment, bool isForTextField = false, int iLineSpacing = 0, bool bBold = false, bool bItalics = false, bool bUnderLine = false);

返回值:static CAImage*

参数:

类型参数名说明
const char*text文字
const char*fontName字体名称
floatfontSize字体大小
const DSize&dimensions图像大小
CATextAlignmenthAlignment文本对齐
CAVerticalTextAlignmentvAlignment垂直文本对齐
boolisForTextField = false是否显示文本框(默认不显示)
intiLineSpacing = 0文本间距(默认0)
boolbBold = false是否显示加粗(默认不加粗)
boolbItalics = false是否显示斜体(默认不斜体)
boolbUnderLine = false是否显示下划线(默认不显示)

解释:创建,并指定文本内容


static int getFontHeight(const char* pFontName, unsigned long nSize);

返回值:static int

参数:

类型参数名说明
const char*pFontName字体名字
unsigned longnSize字体大小

解释:获得字体高度


static int getStringWidth(const char* pFontName, unsigned long nSize, const std::string& pText); 

返回值:static int

参数:

类型参数名说明
const char*pFontName字体名字
unsigned longnSize字体大小
const std::string&pText文本内容

解释:获得字体宽度


static int cutStringByWidth(const char* pFontName, unsigned long nSize, const std::string& text, int iLimitWidth, int& cutWidth);

返回值:static int

参数:

类型参数名说明
const char*pFontName字体名字
unsigned longnSize字体大小
const std::string&pText文本内容
intiLimitWidth最大宽度
int&cutWidth减少的宽度

解释:减少字符串的宽度


static int getStringHeight(const char* pFontName, unsigned long nSize, const std::string& pText, int iLimitWidth, int iLineSpace = 0, bool bWordWrap = true);

返回值:static int 

参数:

类型参数名说明
const char*pFontName字体名字
unsigned longnSize字体大小
const std::string&pText文本内容
intiLimitWidth最大宽度
intiLineSpace = 0间距
boolbWordWrap = true自动换行

解释:减少字符串的高度


static CAImage* scaleToNewImageWithImage(CAImage* image, const DSize& size);

返回值:static CAImage*

参数:

类型参数名说明
CAImage*image图像
const DSize&
size大小

解释:缩放当前图像做新图像


static CAImage* scaleToNewImageWithImage(CAImage* image, float scaleX, float scaleY);

返回值:static CAImage*

参数:

类型参数名说明
CAImage*image图像
floatscaleX缩放X轴
floatscaleY缩放Y轴

解释:缩放当前图像做新图像


static CAImage* generateMipmapsWithImage(CAImage* image);

返回值:static CAImage*

参数:

类型参数名说明
CAImage*image图像

解释:生成mipmap图像


static CAImage* createWithRawDataNoCache(const unsigned char * data, const CAImage::PixelFormat& pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh);

返回值:static CAImage* 

参数:

类型参数名说明
const unsigned char *data数据
const CAImage::PixelFormat&pixelFormat像素格式
unsigned intpixelsWide像素宽
unsigned intpixelsHigh像素高

解释:创建,并指定Raw Data,默认没有缓存


static CAImage* createWithRawData(const unsigned char * data, const CAImage::PixelFormat& pixelFormat, nsigned int pixelsWide, unsigned int pixelsHigh, const std::string& key);

返回值:static CAImage*

参数:

类型参数名说明
const unsigned char *data数据
const CAImage::PixelFormat&pixelFormat像素格式
unsigned intpixelsWide像素宽
unsigned intpixelsHigh像素高
const std::string&key属性

解释:创建,并指定Raw Data,默认有缓存


bool initWithImageFile(const std::string& file);

返回值:bool

参数:

类型参数名说明
const std::string&file图像名

解释:初始化,并指定其图像文件


bool initWithImageFileThreadSafe(const std::string& fullPath);

返回值:bool

参数:

类型参数名说明
const std::string&fullPath完整路径

解释:初始化,并指定其图像文件(支持子线程)


bool initWithImageData(const unsigned char * data, unsigned long dataLen);

返回值:bool

参数:

类型参数名说明
const unsigned char *data数据
unsigned longdataLen数据长度

解释:初始化,并指定其图像缓存


bool initWithRawData(const unsigned char * data, const CAImage::PixelFormat& pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh);

返回值:bool

参数:

类型参数名说明
const unsigned char *data数据
const CAImage::PixelFormat&pixelFormat像素格式
unsigned intpixelsWide像素宽
unsigned int pixelsHigh像素高

解释:初始化,并指定其原数据


const char* description(void);

返回值:const char*

参数:

解释:获取图像描述


void releaseData();

返回值:void

参数:

解释:释放所有数据


void releaseData(unsigned char ** data);

返回值:void

参数:

类型参数名说明
unsigned char **data数据

解释:释放指定数据


void drawAtPoint(const DPoint& point);

返回值:void

参数:

类型参数名说明
const DPoint&point点的位置

解释:基于某个点显示(大小为图像大小)


void drawInRect(const DPoint& rect);

返回值:void

参数:

类型参数名说明
const DPoint&rect矩形大小

解释:基于某个矩形区域显示


bool initWithETCFile(const char* file);

返回值:bool

参数:

类型参数名说明
const char*file文件名

解释:初始化,并指定其ETC文件


const char* stringForFormat();

返回值:const char*

参数:

解释:格式字符串


bool saveToFile(const std::string& fullPath, bool bIsToRGB = false);

返回值:bool

 参数:

类型参数名说明
const std::string&fullPath完全路径
boolbIsToRGB = false是否用RGB格式保存(默认否)

解释:是否保存成RGB格式


const char* getImageFileType();

返回值:const char*

参数:

解释:获取图像文件类型


float getAspectRatio();

返回值:float

参数:

解释:得到长宽比


CAImage::Format detectFormat(const unsigned char * data, unsigned long dataLen);

返回值: CAImage::Format

参数:

类型参数名说明
const unsigned char *data数据
unsigned longdataLen数据长度

解释:检测格式


void premultipliedImageData();

返回值:void

参数:

解释:进行图像数据


void repremultipliedImageData();

返回值:void

参数:

解释:再进行图像数据


void updateGifImageWithIndex(unsigned int index);

返回值:void

参数:

类型参数名说明
unsigned intindexindex

解释:通过index更新GIF图像


unsigned int getGifImageIndex();

返回值:unsigned int

参数:

解释:获取GIF图像的index


unsigned int getGifImageCounts();

返回值:unsigned int

参数:

解释:获取GIF图像的参数


static void reloadAllImages();

返回值:static void

参数:

解释:重新加载所有图像


CAImageView 属性说明

ImageViewScaleType

类型:CAImageViewScaleType

解释:图像缩放类型。set/get{}。


AnimationImages

类型:CAVector<CAImage*>

解释:动画图像。set/get{}。


AnimationDuration

类型:float

解释:动画间隔的时间。set/get{}。


AnimationRepeatCount

类型:unsigned int

解释:动画重复次数。set/get{}。


CAImageView 方法说明

static CAImageView* create();

返回值:static CAImageView*

参数:

解释:创建,默认Frame为(0,0,0,0)


static CAImageView* createWithImage(CAImage* image);

返回值:static CAImageView*

参数:

类型参数名说明
CAImage*image图像

解释:创建,并指定其Image


static CAImageView* createWithFrame(const DRect& rect);

返回值:static CAImageView*

参数:

类型参数名说明
const DRect&rect区域大小

解释:创建,并指定其Frame,默认Frame为(0,0,0,0)


static CAImageView* createWithCenter(const DRect& rect);

返回值:static CAImageView*

参数:

类型参数名说明
const DRect&rect中心点的位置及大小

解释:创建,并指定其Center,默认Center为(0,0,0,0)


virtual bool init(void);

返回值:virtual bool

参数:

解释:初始化


virtual bool initWithImage(CAImage* image);

返回值:virtual bool

参数:

类型参数名说明
CAImage*image图像

解释:初始化图像


virtual void setImage(CAImage* image);

返回值:virtual void

参数:

类型参数名说明
CAImage*image图像

解释:设置图像


virtual CAImage* getImage(void);

返回值:virtual CAImage*

参数:

解释:获取图像


virtual void setImageAsyncWithFile(const std::string& path);

返回值:virtual void

参数:

类型参数名说明
const std::string&path路径

解释:异步设置图像文件


void startAnimating();

返回值:void

参数:

解释:开始动画


void stopAnimating();

返回值:void

参数:

解释:结束动画


bool isAnimating();

返回值:bool

参数:

解释:查看图像动画

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号