2011-04-08 28 views
2

我有一個形象的看法,我試圖改變imageview的形象,每次方向改變,但圖像不會改變任何人可以幫助我在此無法改變一個的UIImageView

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

}

圖像

回答

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;   
    } 
} 
+0

其iPad實例與iPhone相同。 將768和1024更改爲iPhone大小。 – 2011-04-08 13:28:32

0

你在錯誤的地點改變圖像,

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

應該只返回是□ r支持的接口旋轉。

實現:

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

和改變形象出現。 如果你使用imageNamed創建它,你也不需要釋放tempImage,它已經被自動釋放。

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