ios - MPMediaItemArtwork 和 UITableView

标签 ios objective-c xcode uitableview mpmediaitem

我的大项目遇到了一些问题,所以我创建了一个小项目来进行测试。这是我的 .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 and iOS 7 Screenshots

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

最佳答案

看看我的开源播客应用程序。 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];
     }
  }

关于ios - MPMediaItemArtwork 和 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25348006/

相关文章:

ios - 解析推送 REST API 错误 400 "Bad Request"

ios - XCode 8.1 更新框架不起作用

objective-c - 在通用应用程序上分离 iPhone 和 iPad 类有什么好处吗?

ios - 如何将 UILabel 的字体与另一个启用了 adjustsFontSizeToFitWidth 的字体相匹配?

objective-c - 是否可以在 UIView 类中传递参数

ios - 我可以使用 AppCode 而其他人在我的团队中使用 Xcode 吗?

ios - 您如何将来自 ios 的视频数据发布到 Signed Url 到 Google Cloud Bucket?

ios - 如何解析Type U的NFC数据

objective-c - Ooyala 播放器 (iOS) : how to start in full screen?

ios - XCode 5 vs XCode 6 动画变化?