UIImageView 保持空白
UIImageView remains blank
我创建了一个 ViewController,然后将图像 ViewController 附加到它。
这是我的界面和实现。当我 运行 他们编程时它不显示图像,只有空白视图。
#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。
有关问题的任何建议,即为什么我看不到图像。
问题在这里
//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];
不会为您设置帧大小。
如果您要使用图像的 height/width,则必须使用 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);
已经给出的两个答案都指向了正确的方向,如果你想要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);
我创建了一个 ViewController,然后将图像 ViewController 附加到它。 这是我的界面和实现。当我 运行 他们编程时它不显示图像,只有空白视图。
#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。 有关问题的任何建议,即为什么我看不到图像。
问题在这里
//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];
不会为您设置帧大小。
如果您要使用图像的 height/width,则必须使用 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);
已经给出的两个答案都指向了正确的方向,如果你想要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);