简体   繁体   English

仅显示一些png图像,而其他图像则没有明显原因

[英]Only some png images display, others don't for no apparent reason

I have some code which randomly displays a given .png image and its title.The title logic is working fine. 我有一些代码可以随机显示给定的.png图像及其标题,标题逻辑工作正常。 However, for some reason, only two of the four images display. 但是,由于某些原因,四个图像中只有两个显示。 I've tried changing the order just in case, but only those two images, pic_c and pic_d, ever get displayed. 我试图更改顺序以防万一,但是只有这两个图像pic_c和pic_d会显示。 I've also checked the spelling, and that the files are in resources and exist on the file system. 我还检查了拼写,并且文件在资源中并且存在于文件系统中。 Here's the code which displays the image: 这是显示图像的代码:

    NSMutableArray *fileNameArray = [[NSMutableArray alloc] init];                          

    [fileNameArray addObject:[NSString stringWithFormat: @"pic_a"]];     
    [fileNameArray addObject:[NSString stringWithFormat: @"pic_b"]];     
    [fileNameArray addObject:[NSString stringWithFormat: @"pic_c"]];    
    [fileNameArray addObject:[NSString stringWithFormat: @"pic_d"]];     

    NSMutableArray *titleArray = [[NSMutableArray alloc] init];                          

    [titleArray addObject:[NSString stringWithFormat: @"pic a"]];     
    [titleArray addObject:[NSString stringWithFormat: @"pic b"]];     
    [titleArray addObject:[NSString stringWithFormat: @"pic c"]];    
    [titleArray addObject:[NSString stringWithFormat: @"pic d"]];     


    int index = arc4random() % [fileNameArray count];

    NSString *pictureName = [titleArray objectAtIndex:index];  

    art_title.text = pictureName;    

    NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];


    UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];

    if (img != nil) { // Image was loaded successfully.
        [imageView setImage:img];
        [imageView setUserInteractionEnabled:NO];   
        [img release]; // Release the image now that we have a UIImageView that contains it.
    }

    [super viewDidLoad];

    [fileNameArray release];
    [titleArray release];
}

Any idea why this might be happening? 知道为什么会这样吗?

Recent information: If I remove the titleArray, and use the file name array only, all the images show up. 最新信息:如果我删除titleArray,而仅使用文件名数组,则会显示所有图像。 However, I'd still like to be able to use the titleArray. 但是,我仍然希望能够使用titleArray。

What are your image files actually called? 您的图片文件实际上叫什么? In the code above you don't actually do anything with finleNameArray except count it. 在上面的代码中,除了计算它之外,您实际上没有对finleNameArray做任何事情。 You use the values from titleArray to get the image resource. 您可以使用titleArray中的值来获取图像资源。 I would think you need the following code in there: 我认为您在其中需要以下代码:

NSString *filename = [fileNameArray objectAtIndex:index];

Then change your imagePath to use this instead of pictureName . 然后更改您的imagePath以使用它而不是pictureName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM