由于某种原因,我无法将相册插入UIImageView。它被添加到情节提要中并声明:

@property (strong, nonatomic) IBOutlet UIImageView *displayAlbum;
@synthesize displayAlbum, titleLbl, artist;


在我的代码中:

displayAlbum = [[UIImageView alloc] init];

MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:@"1079287588763212246" forProperty:MPMediaEntityPropertyPersistentID];
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate: predicate];
NSArray *queryResults = [query items];

for (MPMediaItem *song in queryResults) {
MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
[displayAlbum setImage: [artwork imageWithSize:CGSizeMake(displayAlbum.frame.size.width, displayAlbum.frame.size.height)]];
        }


我能够获取其他歌曲信息,因此绝对可以获取正确的歌曲,但是ImageView总是空白。

顺便说一句,如果有人可以帮助我清理上面的代码,尤其是摆脱for循环(无论如何,它总是应该只返回一个结果),那会很棒。谢谢

最佳答案

这是因为您正在使用alloc init创建一个新的图像视图,而不是使用在IB中创建的视图。您应该只删除该alloc初始化行。

10-08 01:35