I'm having trouble with loops. I have one imageView
which displays an artist's image and another imageView
which I would like to display artwork of a random song. BUT, this artwork must not match the artist image. [To clear things up - the artist image is one of the song's artwork].
I'd like to essentially write this in Objective-C for iPhone:
Do {
Get artwork of random song in
songsArray
Until{
image1 != image2;
}
This is what I've tried so far (firstArtwork
is the artist's image and secondArtwork
is the randomly-chosen artwork that must not be the same as firstArtwork
).
-(UIImage *)getSecondImage{
MPMediaItemArtwork *firstArtwork = [self.mediaItem valueForProperty:MPMediaItemPropertyArtwork];
// Choose random song in songsArray + get Artwork
MPMediaItem *myItem = [songTracks objectAtIndex:arc4random() % songsArray.count];
MPMediaItemArtwork *secondArtwork = [myItem valueForProperty:MPMediaItemPropertyArtwork];
// Get images
UIImage *firstImage = [firstArtwork imageWithSize:CGSizeMake(1, 1)];
UIImage *secondImage = [secondArtwork imageWithSize:CGSizeMake(1, 1)];
// IF the two images are the same, repeat this block
// IF NOT, return secondImage
if ([firstImage isEqual:secondImage]){
[self getSecondImage];
}
else{
return secondImage;
}
return secondImage;
}
But this sometimes returns an image that's the same as the artist's image.
I've also tried using an NSPredicate
but I get the error that you can't filter using the MPMediaItemPropertyArtwork.... so I can't use a preciate.
I'm not sure if I'm going about this logically/the correct way. Can anybody help me out and explain where I'm going wrong?