0

I am playing around with Xcode 5 and storyboarding. I'm at the point where I have captured a UIImage using the camera, but I can't figure out how to display the image on the phone after I've captured it. I have specified an IBOutlet UIImageView and set it's value to the image that I captured from the camera, but that doesn't seem to do anything at all.

This is my interface:

@interface HCViewController ()

@property (nonatomic, retain) IBOutlet UIImageView *image;

@end

And this is my didFinishPickingWithMediaInfo method:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Get the picture from the camera
    UIImage *imageFromCamera = [info objectForKey:UIImagePickerControllerEditedImage];

    // Set the IBOutlet UIImageView to the picture from the camera
    // This does nothing as far as I can tell
    self.image = [[UIImageView alloc] initWithImage:imageFromCamera];

    // Let's take a closer look at this
    NSLog(@"%@", self.image);

    // Close the camera view controller
    [self dismissViewControllerAnimated:YES completion:nil];
}

This is what I see in the logger for self.image:

2013-09-10 17:17:45.169 YourMom[6136:60b] <UIImageView: 0x1556fdc0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x155d5b10>>

My storyboard has a View Controller with two different scenes that can be swiped back and forth. Both of the scenes have a "View" with an "Image View" as a sub-item. The Image Views each have "Referencing Outlets" that seem to be connected to the image variable that I defined in my interface. However, simply setting the value of image doesn't change the phone display.

After reading this SO question: How can I change the image displayed in an UIImageView programmatically? I tried [self.image setImage:image], but that didn't appear to do anything either. How do I tell Xcode that I want image to show up in the view?

4

4 回答 4

3

Your mistake is in this:

self.image = [[UIImageView alloc] initWithImage:imageFromCamera];

Because you are using Storyboard your UIImageView will be initialized for you. By executing this line of code you are throwing away an old UIImageView and replacing it with a new UIImageView that has no frame.

You just need to do this:

[self.image setImage:imageFromCamera]

Also, you might be getting the wrong image from info. Try UIImagePickerControllerOriginalImage instead of UIImagePickerControllerEditedImage. Hope this is helpful, cheers!

于 2013-09-10T21:40:46.130 回答
1
// this is your delegate method to set the image taken from your camera

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

 // Get the picture from the camera
    UIImage *imageFromCamera = [info objectForKey:UIImagePickerControllerEditedImage];

 // alloc image view as you have to set it image on it
    UIImageView *imgView = [[UIImageView alloc]init];
    imgView.image = imageFromCamera;

// you can also set the image on button like this 
    UIImage *imageFromCamera = [info objectForKey:UIImagePickerControllerEditedImage];
// imgBtn is UIButton property
    [self.imgBtn setImage:imageFromCamera forState:UIControlStateNormal];

// Let's take a closer look at this
    NSLog(@"%@", self.image);

// Close the camera view controller
    [self dismissViewControllerAnimated:YES completion:nil];

}
于 2013-09-30T09:21:25.890 回答
1

UIImage View has an property called image.

This property is of type UIImage.

So if you have an UIImage, then you can set the property as follows:

imageView.image = image;
于 2013-09-30T09:24:22.033 回答
0

You can try this code to display your image.

imageView.image : UIImage = UIImage(named: "YourImageName")

Hope this can help you with your problems.

于 2015-03-07T16:31:52.677 回答