更改CCSprite的图像会改变其大小?

时间:2012-01-17 06:54:22

标签: ios image switch-statement ccsprite

我正在尝试将CCSprite作为我的iPod音乐播放时的专辑图片精灵,但问题是,当我将图像从NoI​​mage.png更改为实际的专辑图片时,CCSprite似乎改变了尺寸和image比原来的CCSprite小。老实说,我不明白为什么会这样,但也许别人会这样!

无论如何,这里是我如何在init方法中创建CCSprite,

albumArtwork = [[[CCSprite alloc] initWithFile:@"NoImage.png"] autorelease];
        [albumArtwork setScaleX:159 / albumArtwork.contentSize.width];
        [albumArtwork setScaleY:139 / albumArtwork.contentSize.height];
        albumArtwork.position = ccp(320/2, 190);
        [self addChild:albumArtwork z:26];

然后当iPod音乐开始时,我这样做是为了将CCSprite图像从NoI​​mage.png更改为iPod歌曲作品:

UIImage *albumArtworkImage = NULL;
    MPMediaItemArtwork *itemArtwork = [currentItem valueForProperty:MPMediaItemPropertyArtwork];
    if (itemArtwork != nil) {
        albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(albumArtwork.contentSize.width, albumArtwork.contentSize.width)];
    }

    if (albumArtworkImage) {
        CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addCGImage:albumArtworkImage.CGImage forKey:@"albumArtwork"];
        [albumArtwork setTexture: tex];
    } else { // no album artwork
        CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:@"NoImage.png"];
        [albumArtwork setTexture:tex];
    }

有谁知道为什么会这样,如果有的话,我该怎么办呢?

EDIT2 : 这就是我将其浓缩为:

CCTexture2D *tex;
    MPMediaItemArtwork *itemArtwork = [currentItem valueForProperty:MPMediaItemPropertyArtwork];
    if (itemArtwork) {
        UIImage *albumArtworkImage = NULL;
        UIImage *firstImage = [itemArtwork imageWithSize:CGSizeMake(159.0f, 139.0f)];
        albumArtworkImage = [firstImage resizedImage:CGSizeMake(albumArtwork.contentSize.width, albumArtwork.contentSize.height) interpolationQuality: kCGInterpolationHigh];
        albumArtworkImage = [albumArtworkImage roundedCornerImage:8 borderSize:4]; 
        tex = [[CCTextureCache sharedTextureCache] addCGImage:albumArtworkImage.CGImage forKey:@"albumArtwork"];
    } else { // no album artwork
        tex = [[CCTextureCache sharedTextureCache] addImage:@"NoImage.png"];
    }
    [albumArtwork setTexture:tex];

1 个答案:

答案 0 :(得分:2)

 if (albumArtworkImage) {
    CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addCGImage:albumArtworkImage.CGImage forKey:@"albumArtwork"];
    [albumArtwork setTexture: tex];
} else { // no album artwork
    CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:@"NoImage.png"];
    [albumArtwork setTexture:tex];
}
// reset visual size after you set new texture:
[albumArtwork setScaleX:159 / albumArtwork.contentSize.width];
[albumArtwork setScaleY:139 / albumArtwork.contentSize.height];

这意味着您的精灵将始终以159x139像素的大小渲染,与图像大小无关。