我该怎么用CCTexture2D initWithImage替换?

时间:2013-06-16 03:56:44

标签: ios cocos2d-iphone

我应该用?

替换CCTexture2D initWithImage

我有这个方法:

CCTexture2D *noMutableTexture = [[[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]] autorelease];

我跳到定义,发现现在唯一的方法是:

- (id) initWithData:(const void*)data pixelFormat:(CCTexture2DPixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSize:(CGSize)size

- (id) initWithCGImage:(CGImageRef)cgImage resolutionType:(ccResolutionType)resolution

我猜它是后者,但我不知道如何构建CGImageRef,也不知道ccResolutionType是什么。

任何帮助将不胜感激。 THX

2 个答案:

答案 0 :(得分:4)

//! ccResolutionType
typedef enum
{
    //! Unknonw resolution type
    kCCResolutionUnknown,
#ifdef __CC_PLATFORM_IOS
    //! iPhone resolution type
    kCCResolutioniPhone,
    //! RetinaDisplay resolution type
    kCCResolutioniPhoneRetinaDisplay,
    //! iphone5 resolution type
    kCCResolutioniPhone5Display,
    //! iPad resolution type
    kCCResolutioniPad,
    //! iPad Retina Display resolution type
    kCCResolutioniPadRetinaDisplay,

#elif defined(__CC_PLATFORM_MAC)
    //! Mac resolution type
    kCCResolutionMac,

    //! Mac RetinaDisplay resolution type (???)
    kCCResolutionMacRetinaDisplay,
#endif // platform

} ccResolutionType;

是resolutionType的可能值。

所以基于设备类型,iPhone或iPhone视网膜(3.5 / 4英寸),iPad,iPad视网膜。分辨率类型的值会有所不同。

- (id) initWithCGImage:(CGImageRef)cgImage resolutionType:(ccResolutionType)resolution

你可以像这样构建一个CGImageRef:

UIImage *someImage = [UIImage imageNamed:@"someImage.png"];
CGImageRef someImageRef = someImage.CGImage;

您可以像这样查看您的设备类型

-(ccResolutionType)resolutionType{
    static NSInteger ret=-1;

    if (ret != -1) return ret;

    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        if( CC_CONTENT_SCALE_FACTOR() == 2 )
            ret = kCCResolutioniPadRetinaDisplay;
        else
            ret = kCCResolutioniPad;
    }
    else
    {
        if( CC_CONTENT_SCALE_FACTOR() == 2 ){
            BOOL isWideScreenEnabled = (BOOL)(fabs((double)[UIScreen mainScreen].bounds.size.height -
                                                   (double)568) < DBL_EPSILON);

            if (isWideScreenEnabled) {
                ret = kCCResolutioniPhone5Display;
            }
            else ret = kCCResolutioniPhoneRetinaDisplay;
        }
        else
            ret = kCCResolutioniPhone;
    }

       if(ret == -1)
        ret = kCCResolutionUnknown;


    return ret;
}

所以,总的来说,如果你想从UIImage创建一个CCTexture那么

UIImage *someImage = [UIImage imageNamed:@"someImage.png"];
CGImageRef someImageRef = someImage.CGImage;
CCTexture2D *tex = [[CCTexture2D alloc] initWithCGImage:someImageRef resolutionType:kCCResolutionUnknown ];

CCTexture2D *tex = [[CCTexture2D alloc] initWithCGImage:someImageRef resolutionType:[self resolutionType]];

答案 1 :(得分:2)

将纹理添加到纹理缓存时,该方法会返回纹理。来自CCTextureCache的文档:

-(CCTexture2D*) addImage: (NSString*) fileimage:

所以在你的情况下:

CCTexture2D *noMutableTexture = [[CCTextureCache sharedTextureCache] addImage:@"myImage.png"];