2010-10-08 59 views
0

當我通過主頁按鈕將其發送到背景時,我的應用程序出現問題。我會盡力不久而是完全描述它:回到前臺後的UIImage(iphone多任務處理)

我有一個UIImage的指針在@interface宣佈與下列財產:

@property (nonatomic, retain) UIImage *pauseImg; 

這個指針設置爲viewDidLoad這樣宣稱的圖像:

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

然後,我有一個使用該圖像獲得按鈕下面的方法:

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

除非應用程序進入後臺並返回前臺,否則這種方式沒有問題。然後,指針指向一個AvAudioPlayer對象而不是圖像,像的NSLog插入上述示出了(第一行是在進入的背景下,第二線時回到前景,每次調用方法前):

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' 

該應用程序在此行崩潰,因爲pauseImg指向一個錯誤的對象。

[playButton setBackgroundImage:pauseImg forState:UIControlStateNormal]; 

我任何蘋果文件中讀入背景時圖像緩存被清空。導致這種行爲?爲什麼pauseImg指向AvAudioPlayer-Object?對這個問題的任何想法?

由於提前, 尼科斯

回答

3

嘗試

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

,而不是

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

你必須使用存取保留pauseImg,否則會被自動釋放。

+0

+1。另外請注意,UIImage卸載和重新加載應該由iOS4自動處理。 – jv42 2010-10-08 10:29:40

+0

非常感謝,這固定了它! – leMonch 2010-10-08 12:10:22