UIImageView not displaying images

1.9k views Asked by At

For some reason I cannot get an image album into my UIImageView. It is added in storyboard and declared:

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

In my code:

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)]];
        }

I am able to get other song information so its definitely getting the right song, but the ImageView always shows up blank.

On a side note, if anybody could help me clean up the above bit of code, especially get rid of the for loop (it should always only return one result anyways) that would be great. Thanks

2

There are 2 answers

2
rdelmar On BEST ANSWER

It's because you're creating a new image view with alloc init, instead of using the one you created in IB. You should just delete that alloc init line.

0
Reconquistador On

Why are you allocating displayAlbum, when it is IBOutlet? If it is already made in NIB, then by allocating new one, it should not work, as you will have completelly new UIImageView which is not part of your view controller. If it is not made in NIB, then you should add it to your view controller by adding subview of some of your views that are part of your view controller and set some frame for it.

At first, try to comment this line:

displayAlbum = [[UIImageView alloc] init];