OGeek|极客世界-中国程序员成长平台

标题: ios - 添加图像的 NSArray 后出现问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:08
标题: ios - 添加图像的 NSArray 后出现问题

我的应用程序有点问题,对这种事情不熟悉,我有点难以弄清楚发生了什么。

我遇到的错误如下

第一个不显示为红色或黄色错误,它只是我的部分代码下的贪婪文本,在下面的“图 1”中显示

第二部分是数组中的黄色错误,用于选择随机图像显示在UIImageView(图2)

图 1

 NSArray *imageNameArray = [[NSArray alloc] initWithObjects"toppipestyleone.png", "toppipestyletwo.png", "toppipestylethree.png", "toppipestylefour.png", "toppipestylefive.png", nil];

这在我放置 UIImageView 的方法中,我在这个方法中也有代码告诉 UIImageView 从屏幕的右到左滚动,我会在“图3”中发布我的整个方法

图2

PipeTop.image = [UIImage imageNamed:[imageNameArray objectAtIndex:arc4random_uniform([imageNameArray count])]];

图 3

-(void)PlacePipe{



    NSArray *imageNameArray = [[NSArray alloc] initWithObjects"toppipestyleone.png", "toppipestyletwo.png", "toppipestylethree.png", "toppipestylefour.png", "toppipestylefive.png", nil];

    PipeTop.image = [UIImage imageNamed:[imageNameArray objectAtIndex:arc4random_uniform([imageNameArray count])]];


    RandomTopPipePosition = arc4random() %350;
    RandomTopPipePosition = RandomTopPipePosition - 228;
    RandomBottomPipePosition = RandomTopPipePosition + 660;

    PipeTop.center = CGPointMake(340-10, RandomTopPipePosition);
    randomImagebottom.center = CGPointMake(340-10, RandomBottomPipePosition);


}

我认为第二个错误与 32 位和 64 位设备有关,但我无法找到针对我的确切问题的实际解决方案我读到的大多数问题都是关于人们使用 NSZombies?我不太确定那是什么。



Best Answer-推荐答案


1

!真正的错误:来自 kirsteins: "只有第一个数组对象是 NSString @"toppipestyleone.png",其他都是 c 字符串字面量。你应该在它们前面加上 @ 来组成 NSString 字面量。"

他/她删除了他们的答案,尽管它是正确的......

NSArray *imageNameArray = [[NSArray alloc] initWithObjects"toppipestyleone.png", @"toppipestyletwo.png", @"toppipestylethree.png", @"toppipestylefour.png", @"toppipestylefive.png", nil];

2

数组从零开始,count-1 是最后一个索引。关于警告。转换它:

所以:

NSUInteger index = (NSUInteger)arc4random_uniform((int)[imageNameArray count]-1);
PipeTop.image = [UIImage imageNamed:[imageNameArray objectAtIndex:index]];

关于ios - 添加图像的 NSArray 后出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23977925/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4