xcode 尝试初始化一个可变数组,然后使用按名称引用的图像加载它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3518278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:38:55  来源:igfitidea点击:

attempting to initialize a mutable array, and then load it with images that are referenced by name

iphonexcodeios4

提问by Sosullivan

I am tryingto write my first iOS app that is not out of a book (though I guess I am stitching things together like crazy....)

我正在尝试编写我的第一个并非出自一本书的 iOS 应用程序(尽管我想我正在疯狂地将东西拼接在一起......)

At this point, I am attempting to initialize a mutable array, and then load it with images that are referenced by name (they are in with the project.)

此时,我正在尝试初始化一个可变数组,然后使用按名称引用的图像(它们在项目中)加载它。

This is what I have so far, and based upon what I see in the debugger, my array is empty.

这是我到目前为止所拥有的,根据我在调试器中看到的,我的数组是空的。



- (void)viewDidLoad
{
    txtLabel = [[UILabel alloc] init];
    imageView = [[UIImageView alloc] init];
    imageArray = [[NSMutableArray alloc] init];
    seg = [[UISegmentedControl alloc] init];

    imageArray = [[NSMutableArray arrayWithObjects: 
                        [UIImage imageNamed:@"jupiter2.JPG"],
                        [UIImage imageNamed:@"waffles?.JPG"],
                        [UIImage imageNamed:@"enterprise.JPG"],
                        [UIImage imageNamed:@"wrunning.JPG"],
                        [UIImage imageNamed:@"ApolloCSM.JPG"],
                        nil] retain];

    imageView.image = [UIImage imageNamed:@"happy-face.JPG"];
    txtLabel.text = @"Hi there!";

    [super viewDidLoad];
}

Thanks for any and all assistance.

感谢您的任何帮助。

Regards,

问候,

Steve O'Sullivan

史蒂夫·奥沙利文

回答by jtbandes

You have

你有

imageArray = [[NSMutableArray alloc] init];

and then

进而

imageArray = [[NSMutableArray arrayWithObjects: ...

This leaks. Get rid of the first one.

这泄漏。摆脱第一个。

But for the 2nd one, you need to use [[NSMutableArray alloc] initWithObjects:...so that it sticks around.

但是对于第二个,您需要使用[[NSMutableArray alloc] initWithObjects:...它以使其保持不变。

回答by Camilo Sanchez

You need to clarify "when" is that you're seeing the array empty. are you sure that viewDidLoad have been called when you're debugging?

您需要澄清“何时”是您看到数组为空。您确定在调试时调用了 viewDidLoad 吗?

and also... in the code sample you don't need this, by doing it you're creating a memory leak

而且......在代码示例中你不需要这个,这样做你会造成内存泄漏

imageArray = [[NSMutableArray alloc] init]; 

other than that there's nothing wrong with the way you construct the array

除此之外,您构造数组的方式没有任何问题

回答by No one in particular

Try allocating the images on their own line and seeing if you have a pointer:

尝试在自己的行上分配图像并查看您是否有指针:

UIImage *jupImg = [UIImage imageNamed:@"jupiter2.JPG"];

If that's null then double check the file name is correct and that the "Copy Bundle Resources" is actually copying the image to where it can be found.

如果它为空,则仔细检查文件名是否正确,并且“复制捆绑资源”实际上是将图像复制到可以找到的位置。

If you have a pointer try accessing some of its values like the width and height to see if they make sense.

如果你有一个指针,试着访问它的一些值,比如宽度和高度,看看它们是否有意义。

--

——

EDIT

编辑

Wait a bit. I went off and thought about this and remembered a similar issue that I had a while ago.

稍等一会。我离开并考虑了这一点,并想起了我不久前遇到的一个类似问题。

I think the problem is that we're using mutable arrays and not static arrays. The method that you use to init the array is not fully defined for the mutable array.

我认为问题在于我们使用的是可变数组而不是静态数组。用于初始化数组的方法没有完全为可变数组定义。

I think you have to try something like:

我认为您必须尝试以下操作:

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

[imageArray addObject:[UIImage imageNamed:@"jupiter2.JPG"]];
... add other images here ...

After this I'm betting that you now actually have something in your array.

在此之后,我打赌您现在实际上在您的阵列中拥有了一些东西。

回答by GameLoading

Try this..

尝试这个..

-->Remove this. imageArray = [[NSMutableArray alloc] init];
-->Try this.
imageArray = [[NSMutableArray alloc]initWithObjects: 
                        [UIImage imageNamed:@"jupiter2.JPG"],
                        [UIImage imageNamed:@"waffles?.JPG"],
                        [UIImage imageNamed:@"enterprise.JPG"],
                        [UIImage imageNamed:@"wrunning.JPG"],
                        [UIImage imageNamed:@"ApolloCSM.JPG"],
                        nil];

If there is no particular reason

如果没有特别的原因

[super viewDidLoad];

put this on top.

把这个放在上面。