【问题标题】:cannot display UIImage one after one无法一个接一个显示 UIImage
【发布时间】:2012-03-08 14:25:49
【问题描述】:

从今天早上开始我有点卡住了...我试图在另一个图像之后加载图像但是...它不起作用...我什至没有显示我的第一张图像...

这是我的代码:

- (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 时,我将发送到 startImage 另一张图像。

【问题讨论】:

    标签: objective-c ios xcode uiimage


    【解决方案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 ..这将判断图像视图图像是否为空..

    【讨论】:

    • 好的,但我还是有问题。我试图解释我在做什么: checkRessources 首先调用我的方法“startImage:(NSString *)nameFile” 然后当 startImage 完成后,我做你所做的然后调用 checkRessources 但现在 checkRessource 将加载另一个显示的方法视频。问题是:视频显示时首先显示图像......就像视频没有给 uimage 足够的时间来显示
    • 如果我不加载播放视频的方法,则图像加载和显示良好
    • 将延迟更改为更大的值.. 再试一次.. 否则发布你的整个代码.. 两个函数
    • 我想要的是:显示一个 uimage,然后是视频,然后是图像或视频取决于文件的名称(0 或 1 或 2 等...)视频之后的视频就可以了。 .. 但不是图片
    • 我认为您尝试断点并检查图像是否成功添加到屏幕.. addsubview 方法成功通过.. 然后确保仅在 10 秒后再次调用 ..check 资源。 . 使用断点来确认这一点。
    【解决方案2】:

    如果这是你的真实代码,你就有一个无限循环。一种方法调用另一种方法.. 否则:问题出在哪里?

    【讨论】:

    • 这真的是我的真实代码,加载图像后 checkRessource 将加载另一个显示视频的方法。
    • 请出示您的整个问题代码并提出更具体的问题。我们无能为力。
    • 我想要的是:显示一个 uimage,然后是视频,然后是图像或视频取决于文件的名称(0 或 1 或 2 等...)视频之后的视频就可以了。 .. 但不是图片
    • 您应该在每个[self startSlideShow:media][self startImage:media]; 之后返回一个,这样就不会同时启动两个操作
    • 谢谢你!有用 ! :) 另一个问题,只是为了滥用你 :p 我想实施过渡,stackoverflow.com/questions/9616628/… 但我不知道你能帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2018-07-01
    相关资源
    最近更新 更多