user3949643
user3949643

Reputation:

MPMediaItemArtwork and UITableView

I ran into some problems with my big project so I created small project for testing purpose. Here are my .h and .m files.

ViewController.h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *table;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController () {
    NSArray *lib;
    NSMutableArray *artworks;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    artworks = [NSMutableArray new];
    lib = [[MPMediaQuery songsQuery] items];

    for(NSUInteger i = 0 ; i < lib.count; i++){
        [artworks addObject:[lib[i] valueForProperty:MPMediaItemPropertyArtwork]];
    }

    [_table setDelegate:self];
    [_table setDataSource:self];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return artworks.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *item = @"a";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:item];

    if(cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"a"];
    }
    [cell.textLabel setText:@"thing"];
    [cell.imageView setImage:[artworks[indexPath.row] imageWithSize:CGSizeMake(100, 100)]];

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *img = [artworks[indexPath.row] imageWithSize:CGSizeMake(43, 43)];
    NSLog(@"%@", img ? @"" : @"nil");
    [cell.imageView setImage:img];
}

@end

Basically, I collect all MPMediaItemArtworks into NSMutableArray. And then I want to use this array as data source for table. However, on iOS 7 imageWithSize returns nil. On iOS 6 everything works perfectly.

Here is the image of comparison:

iOS 6 and iOS 7 Screenshots

The question is: How to make it work like on iOS 6?

Upvotes: 1

Views: 767

Answers (1)

DavidPhillipOster
DavidPhillipOster

Reputation: 4495

Take a look at my open-source podcast app. http://github.com/DavidPhillipOster/podbiceps I'm able to get artwork with similar code to yours under iOS 7 and 8.

  MPMediaItemArtwork *artwork = cast.artwork;
  UIImage *artworkImage = [artwork imageWithSize:cell.imageSize];
  if (nil == artworkImage) {
     CGSize actualSize = cast.artwork.bounds.size;
     if (0 < actualSize.width && 0 < actualSize.height) {
       artworkImage = [artwork imageWithSize:actualSize];
     }
  }

Upvotes: 2

Related Questions