gpt4 book ai didi

ios - 改变 CCSprite 的图像会改变它的大小吗?

转载 作者:行者123 更新时间:2023-11-28 23:08:05 25 4
gpt4 key购买 nike

我试图让 CCSprite 成为我的 iPod 音乐播放时的专辑插图 Sprite ,但问题是,当我将图像从 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];

最佳答案

 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 像素的大小呈现。

关于ios - 改变 CCSprite 的图像会改变它的大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8890734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com