[英]How can I display a UIImage in a UIImageView?
I am playing around with Xcode 5 and storyboarding. 我正在玩Xcode 5和情节提要。 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. 我到了用相机捕获
UIImage
的地步,但是我不知道在捕获后如何在手机上显示图像。 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. 我已经指定了
IBOutlet UIImageView
并将其值设置为我从相机捕获的图像,但这似乎根本不起作用。
This is my interface: 这是我的界面:
@interface HCViewController ()
@property (nonatomic, retain) IBOutlet UIImageView *image;
@end
And this is my didFinishPickingWithMediaInfo
method: 这是我的
didFinishPickingWithMediaInfo
方法:
- (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
: 这是我在记录器中看到的
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. 我的情节提要有一个具有两个不同场景的View Controller,可以前后滑动。 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. 每个图像视图都有“参考出口”,这些出口似乎与我在界面中定义的
image
变量有关。 However, simply setting the value of image
doesn't change the phone display. 但是,仅设置
image
的值不会改变手机的显示。
After reading this SO question: How can I change the image displayed in an UIImageView programmatically? 看完这个SO问题之后: 如何以编程方式更改UIImageView中显示的图像? I tried
[self.image setImage:image]
, but that didn't appear to do anything either. 我尝试了
[self.image setImage:image]
,但这似乎也没有任何作用。 How do I tell Xcode that I want image
to show up in the view? 如何告诉Xcode我希望
image
显示在视图中?
Your mistake is in this: 您的错误在于:
self.image = [[UIImageView alloc] initWithImage:imageFromCamera];
Because you are using Storyboard your UIImageView
will be initialized for you. 因为您使用的是Storyboard,所以将为您初始化
UIImageView
。 By executing this line of code you are throwing away an old UIImageView
and replacing it with a new UIImageView
that has no frame. 通过执行此行代码,您将丢弃旧的
UIImageView
并替换为没有框架的新UIImageView
。
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
. 尝试使用
UIImagePickerControllerOriginalImage
而不是UIImagePickerControllerEditedImage
。 Hope this is helpful, cheers! 希望这对您有所帮助,加油!
// 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];
}
UIImage View has an property called image
. UIImage View具有称为
image
的属性。
This property is of type UIImage
. 此属性的类型为
UIImage
。
So if you have an UIImage
, then you can set the property as follows: 因此,如果您有
UIImage
,则可以如下设置属性:
imageView.image = image;
You can try this code to display your image. 您可以尝试使用此代码显示图像。
imageView.image : UIImage = UIImage(named: "YourImageName")
Hope this can help you with your problems. 希望这可以帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.