开发者

not able to change image in an UIImageView

开发者 https://www.devze.com 2023-02-22 20:29 出处:网络
I have an image view and I am trying to change image of imageview everytime the orientation changes but the image is not ch开发者_开发百科anging can anybody help me on this

I have an image view and I am trying to change image of imageview everytime the orientation changes but the image is not ch开发者_开发百科anging 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;

}


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消