无法在UIImageView中更改图像

时间:2011-04-08 13:22:44

标签: iphone uiimageview

我有一个图像视图,每次方向改变时我都会尝试更改imageview的图像,但图像没有变化,任何人都可以帮我这个

- (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;

}

3 个答案:

答案 0 :(得分:2)

// 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;          
    }
}

答案 1 :(得分:0)

您正在将图像更改到错误的位置,

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
对于支持的接口旋转,

只应返回YES或NO。

实施:

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

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

答案 2 :(得分:0)

有趣的代码......

您可以在此处创建UIImageView对象:

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

没有证据表明这是在任何地方添加到视图中的。例如:

[self.view addSubView:mainBackgroundImageView];

此外,你支持所有轮换,所以只需在这里返回

- (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];