无法更改 UIImageView 中的图像

发布于 2024-10-31 02:08:37 字数 676 浏览 4 评论 0原文

我有一个图像视图,每次方向改变时我都试图改变图像视图的图像,但图像没有改变任何人都可以帮助我吗

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {


if (!mainBackgroundImageView) {
    mainBackgroundImageView=[[UIImageView alloc] init];
}
    if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
    UIImage *tempImage=[UIImage imageNamed:@"dart-login-image.png"];
    [mainBackgroundImageView setImage:tempImage];
    [tempImage release];
}
else {
    [mainBackgroundImageView setImage:[UIImage imageNamed:@"background-and-header-integerated.png"]];
}

return YES;

}

I have an image view and I am trying to change image of imageview everytime the orientation changes but the image is not changing can anybody help me on this

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {


if (!mainBackgroundImageView) {
    mainBackgroundImageView=[[UIImageView alloc] init];
}
    if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
    UIImage *tempImage=[UIImage imageNamed:@"dart-login-image.png"];
    [mainBackgroundImageView setImage:tempImage];
    [tempImage release];
}
else {
    [mainBackgroundImageView setImage:[UIImage imageNamed:@"background-and-header-integerated.png"]];
}

return YES;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

去了角落 2024-11-07 02:08:37
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    NSLog(@"interfaceOrientation");
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight,UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown);
    // take out the ones you DON'T want to be available
}

// if you want to change things based on orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration 
{
    switch (interfaceOrientation) 
    {
        case UIInterfaceOrientationPortrait:
        {       
            //changes for Portait
            NSLog(@"Portait");
            [imgView setFrame:CGRectMake(0,0,768,1024)];
            [imgView setImage:[UIImage imageNamed:@"1.jpg"]];
        }
            break;

        case UIInterfaceOrientationPortraitUpsideDown: 
        {
            //changes for PortaitUpsideDown
            NSLog(@"PortaitUpsideDown");
            [imgView setFrame:CGRectMake(0,0,768,1024)];
            [imgView setImage:[UIImage imageNamed:@"1.jpg"]];
        }
            break;


        case UIInterfaceOrientationLandscapeRight: 
        {
            //changes for LandscapeRight
            NSLog(@"LandscapeRight");
            [imgView setFrame:CGRectMake(0,0,1024,768)];
            [imgView setImage:[UIImage imageNamed:@"2.jpg"]];

        }
            break;

        case UIInterfaceOrientationLandscapeLeft: 
        {
            //changes for LandscapeRight
            NSLog(@"LandscapeRight");
            [imgView setFrame:CGRectMake(0,0,1024,768)];
            [imgView setImage:[UIImage imageNamed:@"2.jpg"]];
        }
            break;          
    }
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    NSLog(@"interfaceOrientation");
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight,UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown);
    // take out the ones you DON'T want to be available
}

// if you want to change things based on orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration 
{
    switch (interfaceOrientation) 
    {
        case UIInterfaceOrientationPortrait:
        {       
            //changes for Portait
            NSLog(@"Portait");
            [imgView setFrame:CGRectMake(0,0,768,1024)];
            [imgView setImage:[UIImage imageNamed:@"1.jpg"]];
        }
            break;

        case UIInterfaceOrientationPortraitUpsideDown: 
        {
            //changes for PortaitUpsideDown
            NSLog(@"PortaitUpsideDown");
            [imgView setFrame:CGRectMake(0,0,768,1024)];
            [imgView setImage:[UIImage imageNamed:@"1.jpg"]];
        }
            break;


        case UIInterfaceOrientationLandscapeRight: 
        {
            //changes for LandscapeRight
            NSLog(@"LandscapeRight");
            [imgView setFrame:CGRectMake(0,0,1024,768)];
            [imgView setImage:[UIImage imageNamed:@"2.jpg"]];

        }
            break;

        case UIInterfaceOrientationLandscapeLeft: 
        {
            //changes for LandscapeRight
            NSLog(@"LandscapeRight");
            [imgView setFrame:CGRectMake(0,0,1024,768)];
            [imgView setImage:[UIImage imageNamed:@"2.jpg"]];
        }
            break;          
    }
}
暮年慕年 2024-11-07 02:08:37

您在错误的位置更改了图像,

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

对于支持的界面旋转,应该只返回 YES 或 NO。

实施:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

并更改那里的图像。
另外,如果您使用 imageNamed: 创建 tempImage,则不需要释放它,它已经自动释放了。

You're changing the image in the wrong place,

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

should only return YES or NO for supported interface rotations.

Implement:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

and change the image there.
Also you don't need to release tempImage if your create it with imageNamed:, it's already autoreleased.

伴我老 2024-11-07 02:08:37

有趣的代码...

您在此处创建一个 UIImageView 对象:

if (!mainBackgroundImageView) {
    mainBackgroundImageView=[[UIImageView alloc] init];
}

没有证据表明它已添加到视图中的任何位置。例如:

[self.view addSubView:mainBackgroundImageView];

此外,您支持所有旋转,因此只需在此处返回 yes

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return YES;
}

将工作放在这里...

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration 
{
   if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
   {
    mainBackgroundImageView.frame = portraitFrame; // set frame size here
    [mainBackgroundImageView setImage:[UIImage imageNamed:@"dart-login-image.png"]];

   } else {

    mainBackgroundImageView.frame = landscapeFrame; // set frame size here
    [mainBackgroundImageView setImage:[UIImage imageNamed:@"background-and-header-integerated.png"]];

}
}

所有这一切都假设您已经将 mainBackgroundImageView 添加到视图中的某个位置,例如 -(void)viewDidLoad 中。

[self.view addSubView:mainBackgroundImageView];

Interesting code...

You create a UIImageView object here:

if (!mainBackgroundImageView) {
    mainBackgroundImageView=[[UIImageView alloc] init];
}

There's no evidence that this is added to the view anywhere. For example:

[self.view addSubView:mainBackgroundImageView];

Additionally, you're supporting all rotations, so just return yes here

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return YES;
}

Put the work here...

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration 
{
   if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
   {
    mainBackgroundImageView.frame = portraitFrame; // set frame size here
    [mainBackgroundImageView setImage:[UIImage imageNamed:@"dart-login-image.png"]];

   } else {

    mainBackgroundImageView.frame = landscapeFrame; // set frame size here
    [mainBackgroundImageView setImage:[UIImage imageNamed:@"background-and-header-integerated.png"]];

}
}

All of this assumes you've already added mainBackgroundImageView to your view somewhere like in -(void)viewDidLoad.

[self.view addSubView:mainBackgroundImageView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文