【问题标题】:Changing image of CCSprite changes its size?更改 CCSprite 的图像会更改其大小?
【发布时间】:2012-01-17 06:54:22
【问题描述】:

我试图让 CCSprite 成为我的 iPod 音乐播放时的专辑插图精灵,但问题是,当我将图像从 NoImage.png 更改为实际专辑插图时,CCSprite 似乎改变了大小并且图像比原来的 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 图像从 NoImage.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];
    }

有谁知道为什么会发生这种情况,如果是,我该如何解决?

编辑2: 这是我将其浓缩为:

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];

【问题讨论】:

    标签: ios image switch-statement ccsprite


    【解决方案1】:
     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];
    

    这意味着您的 sprite 将始终以 159x139 像素的大小呈现,与图像大小无关。

    【讨论】:

    • 看看我的 Edit1,我没有使用我应该使用的 itemArtwork 变量。你似乎遗漏了那部分:P
    • 我认为这与 MPMediaItemArtwork 没有像他们在此线程中所说的那样调整大小有关:stackoverflow.com/questions/7667631/… 你知道我如何将其调整为 159、139 吗?谢谢!
    猜你喜欢
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2014-10-31
    相关资源
    最近更新 更多