开发者

UIImage after coming back to foreground (iphone multitasking)

i have a problem with my app when i send it to background via the home-button. I'll try to descri开发者_如何学编程be it shortly but completely:

I have an UIImage-pointer declared in @interface with following property:

@property (nonatomic, retain) UIImage *pauseImg;

This pointer is set to an image declared in viewDidLoad like this:

 pauseImg = [UIImage imageNamed:@"MP_pause.png"];

Then i have the following method which uses the image for a button:

- (void)playSong:(NSString *)song {
 if (![song isEqualToString:@"0"]) {
  NSString *filePath = [[NSBundle mainBundle] pathForResource:song ofType:@"mp3"];
  NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
  self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
  audioPlayer.currentTime = songPosition;
  [audioPlayer setDelegate:self];
  [audioPlayer setVolume:0.8];
  [audioPlayer play];
  NSLog(@"pauseImage: %@", pauseImg);
  [playButton setBackgroundImage:pauseImg forState:UIControlStateNormal];
  [fileURL release];
 }
}

This works without problems except when the app enters background and comes back to foreground. Then the pointer is pointing to an AvAudioPlayer-object instead of the image, like the NSLog inserted above shows (first line is before entering background, second line when back in foreground, each time calling the method):

2010-10-08 11:41:10.467 CappyBros[864:207] pauseImage: <UIImage: 0x7954f70>
2010-10-08 11:41:23.037 CappyBros[864:207] pauseImage: <AVAudioPlayer: 0x7954f70>
2010-10-08 11:41:23.038 CappyBros[864:207] -[AVAudioPlayer leftCapWidth]: unrecognized selector sent to instance 0x7954f70
2010-10-08 11:41:23.040 CappyBros[864:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVAudioPlayer leftCapWidth]: unrecognized selector sent to instance 0x7954f70'

The app crashes on this line, because pauseImg points to a wrong object.

 [playButton setBackgroundImage:pauseImg forState:UIControlStateNormal];

I read in any apple-document that image-caches are emptied when entering background. Causes this this behaviour? Why does pauseImg then point to an AvAudioPlayer-Object? Any ideas to this problem?

Thanks in advance, Nikos


try

self.pauseImg = [UIImage imageNamed:@"MP_pause.png"];

instead of

pauseImg = [UIImage imageNamed:@"MP_pause.png"];

You have to use the accessor to retain pauseImg, otherwise it is autoreleased.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜