I have a CollectionViewController and a CollectionViewCell. I am fetching data from a database, so when the controller is loaded it dynamically creates the corresponding cells.
Every cell has a UIButton and UITextView.
I am using the UIButton to display a picture (if it exists in the database) or capture an image (if pressed).
InboundCollectionViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
InboundCollectionViewCell *inboundDetailCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InboundDetailCell" forIndexPath:indexPath];
Image *current = [images objectAtIndex:indexPath.row];
[inboundDetailCell.imageType setText:[NSString stringWithFormat:@"%@", [current pd_description]]];
if ([current.pd_image isKindOfClass:[NSData class]] == NO) {
[inboundDetailCell.imageButton addTarget:self action:@selector(useCamera) forControlEvents:UIControlEventTouchUpInside];
}
else {
[inboundDetailCell.imageButton setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}
return inboundDetailCell;
}
So far, so good. I launch my app. The collection view controller populates itself with cells depending on the results from the database.
If the image field has an image, then “check.png” is loaded in the image property of my custom imageButton.
If the image field does not have an image, then the TouchUpInside action for the imageButton is set to the method ‘useCamera’.
- (void)useCamera
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[self presentViewController:imagePicker animated:YES completion:NULL];
}
Now, according to the tutorial I am following, I have to implement the following code:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// set image property of imageButton equal to the value in UIImage 'image' variable ???
[self dismissViewControllerAnimated:YES completion:NULL];
}
In most examples I found, the ImageView and the ImagePickerController are created within the same ViewController. Therefore it is easy to access the image property of the ImageView (or the button in my case).
My problem is that my ‘IBOutlet UIButton imageButton’ is located inside InboundCollectionViewCell, not InboundCollectionViewController. So, I cannot find a way to pass the image returned from the camera, to the image property of my button.
Please note that I am very new to both Objective C and Xcode and this is my first project.. so be gentle! 😛 🙂
Thank you in advance!
Make sure that useCamera receives the button that’s been pressed, and store that in a member variable:
Note that you’ll need to remap the touchUpInside to this function since the signature has changed.
Now, in imagePickerController:didFinishPickingMediaWithInfo: you can access the member variable self.lastButtonPressed to update its image.
Tim