0

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?

Stonz2
  • 6,306
  • 4
  • 44
  • 64
WunDaii
  • 2,322
  • 4
  • 18
  • 26

2 Answers2

0

I think the problem is with your return value. Note that your if statement that checks the value of the 2 images doesn't actually change anything that gets returned to the initial method invoker. Also, your else statement is useless because whether or not your if statement is true, it will then return secondImage. You need to do something like this:

if ([firstImage isEqual:secondImage]){
    secondImage = [self getSecondImage];
}

return secondImage;

This way, you update the value that is returned to the initial invoker.

Stonz2
  • 6,306
  • 4
  • 44
  • 64
  • Thanks for your help, but I loaded the view a few times and secondImage was the same as firstImage a few times. Also, I added an `NSLog(@"firstImage isEqual to secondImage");` to the `if` statement, and it didn't show even though firstImage was equal to secondImage. :S – WunDaii Jul 09 '14 at 21:07
  • Have you tried comparing the `MPMediaItemArtwork` objects instead of the `UIImage`s? – Stonz2 Jul 09 '14 at 21:23
  • The same thing happens - same image appears and the NSLog doesn't show.. I don't understand what's happening :S – WunDaii Jul 09 '14 at 22:04
  • Have you tried something like this to compare your images instead of the built-in `isEqual:`? http://stackoverflow.com/questions/11342897/how-to-compare-two-uiimage-objects – Stonz2 Jul 10 '14 at 14:37
0

Long story short: your pseudo-code is correct (a simple loop), but your implementation contains recursion, which is an unnecessary complexity in this case IMO (and seems that you're not quite sure what you're doing).

-(UIImage *)getSecondImage{

    MPMediaItemArtwork *firstArtwork = [self.mediaItem valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *firstImage = [firstArtwork imageWithSize:CGSizeMake(1, 1)];
    UIImage *secondImage = nil;
    do {
        // Choose random song in songsArray + get Artwork

        MPMediaItem *myItem = [songTracks objectAtIndex:arc4random() % songsArray.count];
        MPMediaItemArtwork *secondArtwork = [myItem valueForProperty:MPMediaItemPropertyArtwork];

        // Get image

        secondImage = [secondArtwork imageWithSize:CGSizeMake(1, 1)];

        // IF the two images are the same, repeat this loop
        // IF NOT, return secondImage
    } while ([firstImage isEqual:secondImage]);
    return secondImage;
}
kambala
  • 2,341
  • 19
  • 20
  • Thanks for your help, but after a few tries the second image was the same as the first image on a few occassions. :S – WunDaii Jul 09 '14 at 21:14
  • 2
    then probably you need to find some other way to compare two `UIImage` objects rather than `-isEqual:` – kambala Jul 09 '14 at 21:25