не может изменить изображение в UIImageView - PullRequest
2 голосов
/ 08 апреля 2011

У меня есть изображение, и я пытаюсь изменить изображение изображения каждый раз, когда меняется ориентация, но изображение не меняется, может кто-нибудь помочь мне в этом

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

2 голосов
/ 08 апреля 2011
// 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 голосов
/ 08 апреля 2011

Интересный код ...

Вы создаете объект 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];
0 голосов
/ 08 апреля 2011

Вы меняете изображение не в том месте,

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

должно возвращать только ДА или НЕТ для поддерживаемых поворотов интерфейса.

Реализация:

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

и измените изображение там.Также вам не нужно выпускать tempImage, если вы создаете его с помощью imageNamed:, оно уже автоматически выпущено.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...