【问题标题】:UIImageView remains blankUIImageView 保持空白
【发布时间】:2015-07-17 19:02:29
【问题描述】:

我创建了一个 ViewController,然后将 ImageViewController 附加到它上面。 这是我的接口和实现。当我运行他们的程序时,它不显示图像,只有一个空白视图。

#import <UIKit/UIKit.h>

@interface ImageViewController : UIViewController
@property (nonatomic, strong) UIImageView *myImageView;

@end

实现

#import "ImageViewController.h"

@interface ImageViewController ()

@end


@implementation ImageViewController

@synthesize myImageView;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //create an image

    //create an image
    UIImage *myScreenShot = [UIImage imageNamed:@"lips_PNG6197.png"];

    //image view instance to display the image
    self.myImageView = [[UIImageView alloc] initWithImage:myScreenShot];

    //set the frame for the image view
    CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width,
                                self.myImageView.frame.size.height/2);
    [self.myImageView setFrame:myFrame];

    //If your image is bigger than the frame then you can scale it
    [self.myImageView setContentMode:UIViewContentModeScaleAspectFit];

    //add the image view to the current view
    [self.view addSubview:self.myImageView];
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

请注意,我在调试器中检查了 UIImage 不是 nil。我也不是通过 IB 创建 UIImageView 。 关于出现问题的任何建议,即为什么我没有看到图像。

【问题讨论】:

    标签: ios uiimageview uiimage


    【解决方案1】:

    如果您没有在 IB 中创建图像视图,那么这就是您的问题:

     CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width,
                                    self.myImageView.frame.size.height/2);
    

    您告诉它的宽度和高度应该基于尚未创建的self.myImageView,所以它是 0。所以这就是您的基本意思:

    CGRectMake(10.0f, 10.0f, 0.0, 0.0);
    

    我假设你可能想要的是这样的:

     CGRect myFrame = CGRectMake(10.0f, 10.0f, self.view.frame.size.width,
                                    self.view.frame.size.height/2);
    

    【讨论】:

    • 正确,还要记住 viewDidLoad 不是设置帧的正确位置,它发生得太早了。您应该在 viewWillAppear 中执行此操作。
    【解决方案2】:

    问题来了

    //set the frame for the image view
    CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width,
                                self.myImageView.frame.size.height/2);
    [self.myImageView setFrame:myFrame];
    

    调试这些行,您会注意到self.myImageView.frame.size.heightself.myImageView.frame.size.width 实际上为零。

    self.myImageView = [[UIImageView alloc] initWithImage:myScreenShot]; 不会为您设置帧大小。

    如果你想使用你必须使用的图像的高度/宽度,myScreenShot.size,如果你想使用视图的框架,那么self.view.frame.size

    【讨论】:

      【解决方案3】:

      已经给出的两个答案都指向正确的方向,如果您希望 imageview 是图像的大小,您可以替换这行代码

      CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width,
                                      self.myImageView.frame.size.height/2);
      

      有了这个

      CGRect myFrame = CGRectMake(10.0f, 10.0f, myScreenShot.size.width,
                                      myScreenShot.size.height);
      

      我不知道你为什么要这样做。使用您熟悉的框架初始化 UIImageView 会更安全,因此可以尝试使用不同的值来适应您的设计。例如

      CGRect myFrame = CGRectMake(10.0f, 10.0f, 300.0f,250.0f);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        相关资源
        最近更新 更多