Objective c 在ios中更改并显示背景图像

Objective c 在ios中更改并显示背景图像,objective-c,uiimageview,uiimage,Objective C,Uiimageview,Uiimage,我正在编写一个应用程序,在其中我以编程方式显示背景图像,然后在顶部更新一些文本 在某些事件中(例如轻触),我想更改文本和背景图像 我能够成功地更改文本,但图像没有更改 我使用以下代码设置图像,其中view,images1和images2是: UIImageView* view; UIImage* images1; UIImage* images2; view = [[UIImageView alloc] initWithImage: image1]; view.frame = CGRectMa

我正在编写一个应用程序,在其中我以编程方式显示背景图像,然后在顶部更新一些文本

在某些事件中(例如轻触),我想更改文本和背景图像

我能够成功地更改文本,但图像没有更改

我使用以下代码设置图像,其中
view
images1
images2
是:

UIImageView* view;
UIImage* images1;
UIImage* images2;

view = [[UIImageView alloc] initWithImage: image1];
view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
[view setUserInteractionEnabled:YES];
[self.view addSubview:view];
[self.view setContentMode:UIViewContentModeScaleToFill];
[self.view sendSubviewToBack:view];
然后我尝试用以下方法改变背景:

view.image = image2;

但是图像没有改变,有什么想法吗?

试试下面的代码来改变点击时的图像手势

首先,您必须像这样在头文件中定义UIImageView

@interface ChangeImageViewController ()
UIImageView *Imgview;
@end
@implementation ChangeImageViewController


Imgview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
Imgview.image=[UIImage imageNamed:@"firstImageName.png"];
[Imgview setUserInteractionEnabled:YES];
[self.view addSubview:Imgview];
[self.view sendSubviewToBack:Imgview];

    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TaponImgview:)];
tapGesture.numberOfTapsRequired=1;
[Imgview addGestureRecognizer:tapGesture];
和在轻拍手势识别方法

-(void)TaponImgview:(UITapGestureRecognizer *)recognizer
{
   Imgview.image=[UIImage imageNamed:@"secondImage.png"]; // Your second image that you want to change it

}

最后,点击UIImageview,您的图像将发生变化。谢谢使用多个视图…image1和image2是什么?图像在哪里?images1和images2使用uiImages1=[UIImageName:@“images/1.jpg]”设置;uiImages2=[UIImageName:@“images/2.jpg”];并且都可以工作,并且都已加载好。您需要初始化
images2
allocinit@Rafouille我还没想到,威尔会研究一下这个想法,然后试试,谢谢。我从文档中认为这将是一个简单的重新分配