UIImagePickerController's object in NSMutableArray
I need to add objects selected by UIImagePicker开发者_如何转开发Controller 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.
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".
Check by casting. Add it in this way:
UIImage *img =[info objectForKey:@"UIImagePickerControllerOriginalImage"];
if(![imagesArray containsObject:img])
[imagesArray addObject:img];
精彩评论