0
我想讓CCSprite成爲我的iPod音樂播放時的專輯圖形精靈,但問題是,當我將圖像從NoImage.png更改爲實際專輯插圖時,CCSprite似乎會更改大小,並且圖像比原來的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音樂開始我這樣做是爲了從NoImage.png的CCSprite圖像切換到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];
看看我的Edit1,我沒有使用我應該的itemArtwork變量。您好像忽略了這一部分:P – 2012-01-17 12:27:08
我認爲它與MPMediaItemArtwork的關係不像他們在此線程中所說的那樣調整大小:http://stackoverflow.com/questions/7667631/mpmediaitemartwork-returning-wrong-sized-artwork 你知道我怎麼能把它調整到159,139嗎?謝謝! – 2012-01-17 12:43:22
看看我的edit1 – 2012-01-17 23:07:36