【问题标题】:MPMediaItemArtwork and UITableViewMPMediaItemArtwork 和 UITableView
【发布时间】:2014-08-17 09:38:12
【问题描述】:

我的大项目遇到了一些问题,所以我创建了一个小项目来进行测试。这是我的 .h 和 .m 文件。

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

基本上,我将所有 MPMediaItemArtworks 收集到 NSMutableArray 中。然后我想用这个数组作为表的数据源。但是,在 iOS 7 上 imageWithSize 返回 nil。在 iOS 6 上一切正常。

这是对比图:

问题是:如何让它像在 iOS 6 上一样工作?

【问题讨论】:

    标签: ios objective-c xcode uitableview mpmediaitem


    【解决方案1】:

    看看我的开源播客应用。 http://github.com/DavidPhillipOster/podbiceps 在 iOS 7 和 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];
         }
      }
    

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多