1

Very simply, I want to take a photo using UIImagePicker and place it into an NSMutableArray. I hve the following code below. The camera part works fine and for test purposes the image is displayed in a UIImage called photoImageView. However I can't seem to add the image into the array. Any help gratefully received.

@property (nonatomic, retain) NSMutableArray *photos;

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *photoTaken = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
photoImageView.image=photoTaken;
[photos addObject:[UIImage photoTaken];
[picker dismissViewControllerAnimated:YES completion:nil];
}
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • Is that your actual code? `[UIImage photoTaken]` would not even compile ... – Martin R Nov 10 '13 at 12:57
  • 1
    possible duplicate of [NSMutableArray addObject not working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working) - #1 in the [nsmutablearray] category. – Martin R Nov 10 '13 at 12:58
  • did you have allocate photos array like photos=[NSMutableArray new]; else what is your output when NSLog the photos array. – Anand Suthar Nov 10 '13 at 15:15
  • @MartinR you are correct it does not compile as is. I have tried so many combinations without joy - hence asking the question. – RGriffiths Nov 10 '13 at 18:58

4 Answers4

1

Have you instantiated the array somewhere (viewDidLoad for example):

self.photos = [NSMutableArray alloc] init];

?

Ian L
  • 5,553
  • 1
  • 22
  • 37
1

1)Are you initialising the photos NSMutableArray? Sometimes addObject: won't throw an error when trying to insert an object into a NULL NSArray.

2)

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *photoTaken = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
    if (photoTaken)
    {
        photoImageView.image=photoTaken;
        [photos addObject:photoTaken];
    }else{
        NSLog(@"Selected photo is NULL");
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
JConway
  • 569
  • 5
  • 13
1

Modify below after adding to (Ian L )

     [photos addObject: photoTaken];
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
1

photoTaken is already instantiated as an UIImage object. No need to do :

[photos addObject : [UIImage photoTaken]];

Simply do this :

[photos addObject : photoTaken];
Sam Fischer
  • 1,442
  • 19
  • 35