Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Captured image not assigned to imageView

When i capture an image i want to assign it to an imageView in another view controller but instead it shows me a white screen. Here's my code:

  • (IBAction)takePhoto:(id)sender {

    AVCaptureConnection *videoConnection = nil; for (AVCaptureConnection *connection in imageOutput.connections) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqualToString:AVMediaTypeVideo]) { videoConnection = connection; break; } } if (videoConnection) { break; } }

    [imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { if (imageDataSampleBuffer != NULL) { NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; UIImage *image = [UIImage imageWithData:imageData]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); ShareViewController *shareView = [ShareViewController new]; [shareView.imageView setImage:image]; // Here i assign the image [self performSegueWithIdentifier:@"imagePush" sender:nil]; } }]; }

Gabe Nadel
seal-mask
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

So, what is the value for image and imageData, if you place a breakpoint at this line:

  ShareViewController *shareView = [ShareViewController new];

Also, what is the value for shareView, if you place a breakpoint at this line:

  [shareView.imageView setImage:image];

Don't really understand the value of imageData i see 1491591 bytes, the image returns the image and share returns shareviewcontroller

Gabe Nadel
seal-mask
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

Can you share the code for ShareViewController.m. Also, did you hook-up your imageView IBOutlet in Interface Builder?

Yes i've done that. Here's the code:

  • (void)viewDidLoad { [super viewDidLoad];

    self.groupField.delegate = self;

    NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Type group name..." attributes:@{ NSForegroundColorAttributeName : [UIColor colorWithRed:10/255.0 green:201/255.0 blue:166/255.0 alpha:100.0] }]; self.groupField.attributedPlaceholder = str; }

  • (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder]; return NO; }

  • (IBAction)shareBtn:(id)sender {

}

  • (IBAction)backBtn:(id)sender {

    [self.navigationController popToRootViewControllerAnimated:YES]; }

A few other answers i found was to set the image to prepareForSegue but i don't know how to do that exactly can you give me hint?

Gabe Nadel
seal-mask
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

So, where are you declaring the imageView property in the class ShareViewController? Can you include the .h file as well?

@interface ShareViewController : UIViewController <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView; @property (weak, nonatomic) IBOutlet UITextField *groupField;

@end

2 Answers

Gabe Nadel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

Can you place a breakpoint after this line:

   NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

and let us know what the value is for imageData

Also, in this line:

  [shareView.imageView setImage:image];

If you instead setImage using an image file bundled inside your project, does it show up correctly? Note, I mean set the image in code, not in Interface Builder.

both nil

Gabe Nadel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

Ok, so now you know the problem doesn't have anything to do with the UIImageView. Instead, it looks like your problem is upstream, so I'd go poking around there. Also, if you are still stuck after looking in that area, perhaps you can repost the code using some indents, so we can read it a bit easier. Also, it'd be good to show us more complete code, like where you create the "imageOutput" object.

Sorry i made a mistake before the image is not nil only the shareVIew.imageView shows nil. I did this based on this tutorial https://www.youtube.com/watch?v=Xv1FfqVy-KM

Only difference is that i want to display the image in another view controller