2

我应该用什么替换 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 是什么。

任何帮助将不胜感激。谢谢

4

2 回答 2

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]];
于 2013-06-16T13:44:30.923 回答
2

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

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

所以在你的情况下:

CCTexture2D *noMutableTexture = [[CCTextureCache sharedTextureCache] addImage:@"myImage.png"];
于 2013-06-16T12:10:32.373 回答