UIImage没有在我的屏幕上显示

时间:2013-05-30 08:49:54

标签: objective-c

以下是我的代码。我可以从我的NSMutableArray文件加载所有图像文件,但它没有出现在我的屏幕上。我真的不知道为什么。如果有人能帮助我,这将是非常好的。

提前致谢.....

UIImageView *bannerImages;
    CGRect bannerFrame;

    for (int photoCount = 0; photoCount < [imagesFileList count]; photoCount++)
    {
        NSLog(@"photoCount: %d",photoCount);

        bannerImages = [[UIImageView alloc] initWithFrame:CGRectMake(0, 340, 320, 80)];

        NSString *photoString = [NSString stringWithFormat:@"%@",[imagesFileList objectAtIndex:photoCount]];

        NSLog(@"photoString are: %@",photoString);

        bannerImages.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:photoString]];

        bannerFrame = bannerImages.frame;
        bannerFrame.origin.y = self.view.bounds.size.height;
    }

    [UIView animateWithDuration:1.0
                          delay:1.0
                        options: UIViewAnimationTransitionCurlDown
                     animations:^{
                         bannerImages.frame = bannerFrame;
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Done!");
                     }];

    [self.view addSubview:bannerImages];
    [bannerImages release];

这是我的日志文件show:

2013-05-30 15:09:19.595 Yangon Guide Map[5035:15803] photoCount: 0
2013-05-30 15:09:19.596 Yangon Guide Map[5035:15803] photoString are: adv01.png
2013-05-30 15:09:19.596 Yangon Guide Map[5035:15803] photoCount: 1
2013-05-30 15:09:19.596 Yangon Guide Map[5035:15803] photoString are: adv02.png
2013-05-30 15:09:19.596 Yangon Guide Map[5035:15803] photoCount: 2
2013-05-30 15:09:19.596 Yangon Guide Map[5035:15803] photoString are: adv03.png
2013-05-30 15:09:19.597 Yangon Guide Map[5035:15803] photoCount: 3
2013-05-30 15:09:19.597 Yangon Guide Map[5035:15803] photoString are: edupro.png
2013-05-30 15:09:21.598 Yangon Guide Map[5035:15803] Done!

3 个答案:

答案 0 :(得分:0)

1。假设.png图片位于App Bundle,您可以替换

bannerImages.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:photoString]];

bannerImages.image = [UIImage imageNamed:photoString]];

2。 bannerFrame.origin.y = self.view.bounds.size.height;似乎没什么帮助。

答案 1 :(得分:0)

我认为问题在于,您已经使用在日志记录时获得的图像名称填充了imageFileList。

imageWithContentsOfFile不应该得到像这样的图像

尝试

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:photoString ofType:@"png"]]];

你可以使用非常紧张的imageNamed methiod,而不是这个

bannerImages.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:photoString]];

使用此

bannerImages.image = [UIImage imageNamed:photoString];

答案 2 :(得分:0)

你真的想做什么?因为他们的代码中存在很多错误......

1)你没有创建你的UIImage -imageWithContentsOfFile:找不到你的文件,使用:

bannerImages.image = [UIImage imageNamed:photoString];

bannerImages.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[imageNamed:photoString stringByDeletingPathExtension] ofType:@"png"]];

2)您为阵列中的每个图像创建一个新的UIImageView,但只将最后一个添加到根视图中。在你的每个UIImageView中添加循环:

for (int photoCount = 0; photoCount < [imagesFileList count]; photoCount++)
{
     bannerImages = [[UIImageView alloc] initWithFrame:CGRectMake(0, 340, 320, 80)];

     // Snip ...

[UIView animateWithDuration:1.0
                      delay:1.0
                    options: UIViewAnimationTransitionCurlDown
                 animations:^{
                     bannerImages.frame = bannerFrame;
                 }
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }];

[self.view addSubview:bannerImages];
[bannerImages release];
}

3)你的bannerFrame计算似乎也是错误的,因为你的超出视图范围,所有UIImageView都有相同的框架。您的超级视图是什么,UIScrollView?你想做什么?