2014-09-23 49 views
5

雖然封面是在蘋果音樂應用程序中顯示的,但「圖片」始終爲空(「null」)。 在iOS 7中可以正常工作,但是在iOS 8中我沒有任何掩護。MPMediaItemArtwork爲空,但封面在iTunes中可用

我的代碼有什麼問題,或者iOS 8中發生了什麼變化?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    AlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"]; 
    MPMediaItemCollection *song = self.songsList[indexPath.row]; 

    cell.albumName.text = [[song representativeItem] valueForProperty: MPMediaItemPropertyAlbumTitle]; 

    cell.albumArtist.text = [[song representativeItem] valueForProperty:MPMediaItemPropertyAlbumArtist]; 


    CGSize artworkImageViewSize = CGSizeMake(100, 100); 
    MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork]; 
    UIImage *image = [artwork imageWithSize:artworkImageViewSize]; 

    NSLog(@"-------------------"); 
    NSLog(@"%@",[[song representativeItem] valueForProperty: MPMediaItemPropertyAlbumTitle]); 
    NSLog(@"%@",image); 

    if (artwork) { 

     cell.cover.image = image; 
    } 
    else 
    { 
     cell.cover.image = [UIImage imageNamed:@"nocover.png"]; 
    } 

    return cell; 
} 

回答

0

你的代碼看起來不錯。我有相同的代碼獲取藝術品,它在iOS 8上爲我工作。你確定這個項目實際上有藝術作品嗎?在音樂應用程序中播放該歌曲 - 藝術作品?

+0

是的,當我玩的音樂應用程序相同的歌曲或專輯,作品在那裏。 – Meins 2014-09-24 07:37:35

+0

作品!=無但是圖像==無 imageWithSize有問題嗎? – Meins 2014-09-24 07:51:11

+0

我試過它在一個新的項目,它不工作,這裏的項目 http://www.file-upload.net/download-9572664/TestCover.zip.html – Meins 2014-09-24 08:18:33

2

我發現了這個問題。

it's

CGSize artworkImageViewSize = CGSizeMake(100, 100); 

了以下工作:

CGSize artworkImageViewSize = CGSizeMake(120, 120); 

CGSize artworkImageViewSize = CGSizeMake(60, 60); 

的一切,可以通過3個作品進行劃分。

+0

我想你會發現它是倍數從60,60,120,240,360(?),480 ....等開始。 90不會工作,而360也不會。 – topLayoutGuide 2014-11-16 08:42:35

10

從iOS 8開始,MPMediaItem的選擇器imageWithSize:(CGSize)size似乎不能保證它會返回圖像。如果沒有圖像顯示在所請求的大小返回,與藝術品bounds屬性的大小又稱之爲:

MPMediaItemArtwork *artwork = [self valueForProperty:MPMediaItemPropertyArtwork]; 
UIImage *image = [artwork imageWithSize:size]; 
if (image == nil) { 
    image = [artwork imageWithSize:artwork.bounds.size]; 
} 
+1

這應該是被接受的答案。 – werm098 2015-07-13 01:11:01

相關問題