我的大项目遇到了一些问题,所以我创建了一个小项目来进行测试。这是我的 .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 上一样工作?
最佳答案
看看我的开源播客应用程序。 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/