我试图以编程方式将UIImageView添加到我的模态视图控制器中,图像根本不会显示。我只看到白色背景。其余工作正常,因为我可以加载或关闭视图。我的代码如下:
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)];
imageView.image = [UIImage imageNamed:@"IMG_0352.PNG"];
[self.view addSubview:imageView];
UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeleft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swiperight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
UIBarButtonItem *dismiss_btn = [[UIBarButtonItem alloc] initWithTitle:@"Start App" style:UIBarButtonItemStylePlain target:self action:@selector(dismissModal:)];
self.navigationItem.rightBarButtonItems = [NSMutableArray arrayWithObjects:dismiss_btn, nil];
答案 0 :(得分:3)
这一行存在一个严重的问题:
UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)];
需要:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
您额外拨打了init
。
还要确保文件名真的命名为IMG_0352.PNG
。案件很重要。确保它不是IMG_0352.png
或类似的东西。
当然,请确保您确实将这样的图像打包到您的应用中。确保它列在目标的“构建阶段”的“复制包资源”部分下。
答案 1 :(得分:0)
你需要放弃
UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)];
因为......好吧...... init] initWithFrame:
看起来像是一个错字,以下是一个更好的方法来初始化 UIImageView
:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMG_0352.PNG"]];
答案 2 :(得分:0)
imageView
初始化两次。尝试删除一个init
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
同时将图像添加到图像文件夹时,检查图像/文件夹是否在Xcode中添加为“创建组”选项而不是“创建文件夹参考”。 文件夹引用只会提供一个链接或引用,因此您无法实际访问该文件夹中的元素。