简体   繁体   中英

UIImageView switching setContentMode random EXEC_BAD_ACCESS

I'm using an UIImageView and UIScrollView inside UIViewController to show images. Actual Size and Fit to Screen options available at the toolbar to let user switch between modes. While switching between modes I am getting EXEC_BAD_ACCESS error randomly. Randomly means I can switch to Actual Size mode to Fit to Screen modes copule of times. When I hit one of them randomly application crashes.

Here is the code that I am using if it wont help I may post the h and m files.

        -(void)loadImage{

     NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:imgFilePath];

     imageView.image = [UIImage imageWithData: [NSData dataWithContentsOfFile:filePath]];

     imgLoaded = 1;

    }


    -(void)FitToScreen{

     if(imgLoaded<1) [self loadImage];

     if(currentState != STATE_IMGPLAYER_FULLSCREEN ){
      currentState = STATE_IMGPLAYER_FULLSCREEN;
     //[imageView setContentMode:UIViewContentModeCenter];
      [imageView setContentMode:UIViewContentModeScaleToFill];
      CGSize photoSize = imageView.frame.size;
      [scrollView setContentSize:photoSize];
     }
     // 

    }


    -(void)ActualSize{


     if(currentState != STATE_IMGPLAYER_ACTUAL_SIZE){

      currentState = STATE_IMGPLAYER_ACTUAL_SIZE;

      [imageView setContentMode:UIViewContentModeTopLeft];
      [scrollView setContentMode:UIViewContentModeCenter];

      CGSize photoSize = [imageView.image size];

      [scrollView setContentSize:photoSize];

     }

    }

-(IBAction)info_clicked:(id)sender 
{
 UIButton *button = (UIButton *)sender;


 if ([button tag] == BTN_FIT2SIZE) {

  [Button setEnabled:NO];
  [Button2 setEnabled:YES];

  [self ActualSize];

 } else if ([button tag] == BTN_FIT2SCREEN) {

  [Button setEnabled:YES];
  [Button2 setEnabled:NO];
  [self FitToScreen];

 }

 [button release];
}

Console Msg:

Program received signal:  “EXC_BAD_ACCESS”.

Thanks for your help.

A first quick guess is that maybe you aren't retaining the imageView or its image? If you could post your stack trace and maybe the line where it crashes, it would be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM