2013-08-25 4 views
5

voglio arrivare la copertina dell'album da un audiolibro nella libreria iPod, ma non capisco come usare MPMediaItemPropertyArtworkiOS: Come ottenere e visualizzare le copertine degli album da iPod biblioteca

- (NSArray *)audiobooks 
{ 
    MPMediaPropertyPredicate *abPredicate = 
    [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeAudioBook] 
            forProperty:MPMediaItemPropertyMediaType]; 

    MPMediaQuery *abQuery = [[MPMediaQuery alloc] init]; 
    [abQuery addFilterPredicate:abPredicate]; 
    [abQuery setGroupingType:MPMediaGroupingAlbum]; 
    NSArray *books = [abQuery collections]; 
    return books; 
} 

- (MPMediaItem *)mediaItemForRow: (NSInteger)row 
{ 
    NSArray *audiobooks = self.audiobooks; 
    MPMediaItem *mediaItem = nil; 

    for (id object in audiobooks) { 
     if ([object isKindOfClass:[MPMediaItemCollection class]]) { 
      MPMediaItemCollection *book = (MPMediaItemCollection *)object; 

      id item = [book items][row]; 
      if ([item isKindOfClass:[MPMediaItem class]]) { 
       mediaItem = (MPMediaItem *)item; 

      } 
     } 
    } 
    return mediaItem; 
} 

Così ora posso ottenere il titolo di un elemento multimediale come questo:

Ma come ottengo l'immagine così posso visualizzarla in una UIImmagine? c'è questa proprietà:

[mediaItem valueForProperty:MPMediaItemPropertyArtwork] 

Non sono riuscito a scoprire come usarlo.

+0

possibile duplicato del [Non in grado di ottenere l'UIImage da MPMediaItemPropertyArtWork] (http://stackoverflow.com/questions/6604829/not-able-to-get-the-uiimage-from-mpmediaitempropertyartwork) –

+0

Qui è un riferimento utile: https://stackoverflow.com/questions/28979013/how-do-i-get-the-album-artwork-of-the-currently-playing-music-using-swift/48035857#48035857 –

risposta

9

Si dovrebbe essere in grado di recuperare l'opera d'arte in un UIImage tramite il seguente:

MPMediaItemArtwork *itemArtwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork]; 
UIImage *artworkUIImage = [itemArtwork imageWithSize:CGSizeMake(64, 64)]; 

In sostanza, la proprietà MPMediaItemPropertyArtwork restituisce un MPMediaItemArtwork che è quindi possibile ottenere un UIImage da.

+0

Grazie ! Come nota, il metodo imageWithSize sembra non ridimensionare l'immagine, ma restituire invece una dimensione fissa di 64x64 o 48x48, forse qualcosa a che fare con la mia cella tableView, ma non la penso così. –

+0

@TomLilletveit Come suggerito, sospetto che sia UITableViewCell a determinare la dimensione visualizzata di UIImage. –

1

ho appena convertito la risposta di cui sopra per Swift 3:

let songInfo: MPMediaItem = self.arrSongs[indexPath.row] as! MPMediaItem 

// Info canzone è Media Item.

let itemArtwork :MPMediaItemArtwork = songInfo.artwork! 
cell.songImg.image = itemArtwork.image(at: CGSize(width: 50, height: 50)) 
Problemi correlati