简体   繁体   English

如何在UIImageView中显示UIImage?

[英]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.

相关问题 如何将一个大的tif文件分解为多个UIImage,以显示在UIImageView中? - How can I break up a large tif file into multiple UIImage's for display in a UIImageView? 如何在不使用UIImageView的情况下缩小UIImage? - How I can pinch zoom UIImage without using UIImageView? 如何像iPhoto中一样将UIImageView的滚动限制在UIImage的边界上? - How can I restrict the scrolling of a UIImageView to the bounds of the UIImage like in iPhoto? 如何在单元格中显示UIImage数组? - How can I display UIImage array in cell? 我该如何解决“无法分配&#39;UIImageView类型的值?” 键入“ UIImage?”问题? - How can I solve “Cannot assign value of type 'UIImageView?' to type 'UIImage?'” issue? 如何在CAShape图层顶部显示UIImage? - How can I display a UIImage on top of a CAShape Layer? 如何显示图片网址中的UIImage - how can I display UIImage from image url 如何将UIImageView上的触摸限制为其UIImage的不透明部分? - How do I restrict touches on a UIImageView to the opaque parts of its UIImage? 如何像素化和取消UIImage或UIImageview的像素化? - How to pixelize and unpixelize an UIImage or UIImageview? 我有一个UIImage,它是在UIImageView中绘制黑白图像的线。 如何在触摸屏上填充颜色? - I have an UIImage that is a line drawn black & white image in a UIImageView. How can I fill inside of the lines with color on touch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM