Ios UIImageView仍为空

Ios UIImageView仍为空,ios,uiimageview,uiimage,Ios,Uiimageview,Uiimage,我创建了一个ViewController,然后将ImageViewController连接到它。 下面是我的接口和实现。当我运行他们的程序时,它不会显示图像,只有一个空白视图 #import <UIKit/UIKit.h> @interface ImageViewController : UIViewController @property (nonatomic, strong) UIImageView *myImageView; @end 注意,我在调试器中检查了UIImag

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

#import <UIKit/UIKit.h>

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

@end
注意,我在调试器中检查了UIImage不是nil。我也不是通过IB创建UIImageView。 任何关于哪里出了问题的建议,例如为什么我看不到图像。

问题就在这里

//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.height
self.myImageView.frame.size.width
实际上为零

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

如果您希望使用必须使用的图像高度/宽度,
myScreenShot.size
,如果您希望使用视图的边框,那么问题就在这里

//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.height
self.myImageView.frame.size.width
实际上为零

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


如果您希望使用必须使用的图像高度/宽度,
myScreenShot.size
,如果您希望使用视图的框架,则
self.view.frame.size

如果您没有在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);

如果您没有在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);

已经给出的两个答案都指向正确的方向,如果希望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更安全,因此可以尝试使用不同的值来适合您的设计。e、 g

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

已经给出的两个答案都指向正确的方向,如果希望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更安全,因此可以尝试使用不同的值来适合您的设计。e、 g

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

正确,还要记住viewDidLoad不是设置帧的正确位置,它发生得太早了。您应该在ViewWillDisplay中执行此操作。正确,还要记住viewDidLoad不是设置帧的正确位置,它发生得太早。您应该在视图中执行此操作。