2012-03-08 73 views
0

我今天早上被卡住了...我試圖加載一個圖像後,另一個圖像,但...它不工作...我甚至沒有我的第一個圖像顯示...不能顯示UIImage一個接一個

有我的代碼:

- (void) checkResources 
{ 

    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot]; 


    NSString *cuttedName; 
    NSString *cuttedName1; 



    for (NSString *path in direnum) 
    { 

     if ([[path pathExtension] isEqualToString:@"mp4"]) 
     { 
      cuttedName1 = [path stringByReplacingOccurrencesOfString:@"ressources/" withString:@""]; 
      cuttedName = [cuttedName1 stringByReplacingOccurrencesOfString:@".mp4" withString:@""]; 
      if ([cuttedName isEqualToString:[NSString stringWithFormat:@"%d", num]]) 
      { 
       NSString *media = [NSString stringWithFormat:@"%@/%@", bundleRoot, path]; 
       [self startSlideShow:media]; 
      } 
     } 
     else if ([[path pathExtension] isEqualToString:@"mov"]) 
     { 
      cuttedName1 = [path stringByReplacingOccurrencesOfString:@"ressources/" withString:@""]; 
      cuttedName = [cuttedName1 stringByReplacingOccurrencesOfString:@".mov" withString:@""]; 
      if ([cuttedName isEqualToString:[NSString stringWithFormat:@"%d", num]]) 
      { 
       NSString *media = [NSString stringWithFormat:@"%@/%@", bundleRoot, path]; 
       [self startSlideShow:media]; 
      } 
     } 
     else if ([[path pathExtension] isEqualToString:@"png"]) 
     { 
      cuttedName1 = [path stringByReplacingOccurrencesOfString:@"ressources/" withString:@""]; 
      cuttedName = [cuttedName1 stringByReplacingOccurrencesOfString:@".png" withString:@""]; 
      if ([cuttedName isEqualToString:[NSString stringWithFormat:@"%d", num]]) 
      { 
       NSString *media = [NSString stringWithFormat:@"%@/%@", bundleRoot, path]; 
       num++; 
      [self startImage:media]; 
      } 
     } 
    } 

} 
- (void) startImage:(NSString *)nameFile 
{ 
    CGRect myImageRect = CGRectMake(0, 0, 200, 500); 

    UIImageView *imageView; 
    imageView = [[UIImageView alloc] initWithFrame:myImageRect]; 
    [imageView setImage:[UIImage imageNamed:@"0.png"]]; 
    NSLog(@"IAMGE %@", imageView.image); 
    [self.view addSubview:imageView]; 
    [self performSelector:@selector(checkResources) withObject:nil afterDelay:10]; // this will give time for UI to update itself.. 
    [imageView release]; 

    } 

- (void) startSlideShow:(NSString *)nameFile 
{  
    NSURL *url = [NSURL fileURLWithPath:nameFile];//[[NSBundle mainBundle] pathForResource:@"2" ofType:@"mov"]]; 




    MPMoviePlayerController *moviePlayer = 
    [[MPMoviePlayerController alloc] initWithContentURL:url]; 
    // NSLog(@" URL : %@", url); 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:moviePlayer]; 
    moviePlayer.view.frame = CGRectMake(0, 0, 500, 600); 

    moviePlayer.controlStyle = MPMovieControlStyleDefault; 
    moviePlayer.shouldAutoplay = YES; 
    [self.view addSubview:moviePlayer.view]; 
    [moviePlayer setFullscreen:YES animated:YES]; 
    } 



-(void)moviePlayBackDidFinish: (NSNotification*)notification 
{ 
    MPMoviePlayerController *moviePlayer = [notification object]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self  
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:moviePlayer]; 

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) 
    { 
     // [moviePlayer.view removeFromSuperview]; 
    } 


    //[[moviePlayer release] addAnimation:animation forKey:kCATransition]; 


    num++; 
    [self checkResources]; 
} 

不要介意我的代碼的目標:)我只是想顯示的UIImage,那麼當被調用後顯示的方法checkResources我會發送第一張圖片來開始圖片另一張圖片。

回答

1

兩個主要的問題,可以在你的代碼..沒有其他問題可以。

1)由於連續addSubview方法您的UI不會更新..

2)你的UIImage爲空..

要檢查1)

變化

[self checkResources]; 

[self performselector:@selector(checkResources) withObject:nil afterDelay:3] // this will give time for UI to update itself.. 

使用自動完成上面的代碼...可能是拼寫錯誤..

要檢查2)

簡單NSlog imageview.image ..這將告訴我們,如果圖像視圖圖像爲空或不..

+0

好吧,我做了,但我仍然有問題。我試圖解釋我在做什麼: checkRessources調用首先我的方法「startImage:(NSString *)nameFile」 然後,當startImage完成時,我做了你所做的然後checkRessources被調用,但現在checkRessource將加載另一種顯示視頻的方法。 問題是:顯示的視頻先不顯示圖像...它就像視頻沒有給足夠的時間來顯示圖像 – user1256827 2012-03-08 16:19:48

+0

如果我不加載播放視頻的方法圖像是好的加載和顯示 – user1256827 2012-03-08 16:25:28

+0

將延遲更改爲更大的值..並再試一次..其他明智地發佈您的整個代碼..這兩個函數 – Shubhank 2012-03-08 16:28:32

1

如果這是你的真實代碼,你有一個無限循環。一種方法稱爲另一種方法.. 否則:問題在哪裏?

+0

它真的是我真正的代碼,加載圖像後,checkRessource會加載另一個顯示視頻的方法。 – user1256827 2012-03-08 16:25:02

+0

請顯示您的問題的整體代碼並詢問更具體的問題。我們不能這樣幫忙。 – calimarkus 2012-03-08 16:28:33

+0

我發佈了整個代碼。感謝您的幫助 – user1256827 2012-03-08 16:30:17