0

I need to add objects selected by UIImagePickerController into an NSMutabeArray, the code goes as:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissModalViewControllerAnimated:YES];

    if(![imagesArray containsObject:[info objectForKey:@"UIImagePickerControllerOriginalImage"]])
        {
        [imagesArray addObject:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];

        } 
    else {
           //some procedures
            }
}

The problem is that it always fells into the if block and never executes the else block when picking same images repeatedly. Any clue over this would be appreciated.

Syed Absar
  • 2,274
  • 1
  • 26
  • 45

2 Answers2

1

I think your problem is that two instances of UIImage can't be compared using isEqual:, which containsObject: is using, even if they point to the same image file. I don't know how to compare two UIImage but you can perhaps look at Generate hash from UIImage or search for "comparing UIImage".

Community
  • 1
  • 1
Robert Höglund
  • 10,010
  • 13
  • 53
  • 70
  • Thank you. i solved it using the 'less than optimal solution' in your provided url. Thanks alot for the help. – Syed Absar Nov 05 '10 at 07:03
0

Check by casting. Add it in this way:

UIImage *img =[info objectForKey:@"UIImagePickerControllerOriginalImage"];
if(![imagesArray containsObject:img])
 [imagesArray addObject:img];
Vaishnavi Naidu
  • 2,625
  • 3
  • 25
  • 26