为数组中的每个项目添加单独的 UIImageViews [英] Adding individual UIImageViews for each item in an array

查看:66
本文介绍了为数组中的每个项目添加单独的 UIImageViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为数组中的每个项目添加一个单独的 UiImageView,这是我目前所拥有的

I am trying to add a individual UiImageView for each item in an array this is what I have so far

_shapeArray = [NSArray arrayWithObjects:@"cloud.png",@"star.png", nil];

    for (int i = 0; i < [_shapeArray count]; i++){
        // I make a UIImage, which I will draw later
        UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_shapeArray objectAtIndex:i]]];

        UIImageView* blockView = [[UIImageView alloc] initWithImage:image];

        blockView.frame = CGRectMake(arc4random()%320, arc4random()%460, image.size.width, image.size.height);

        [self.view addSubview:blockView];

    }

但正如你所知道的,它只是添加了数组中的最后一个图像.我想不出一种方法来将数组对象编号添加到 UIImageView 的名称中.也许我的方法不对,如果是这样,最好的方法是什么?

But as you can tell it just adds the last image in the array. I can not figure out a way to maybe add the array object number to the name of the UIImageView. Maybe I am going at it the wrong way, if so what would be the best way?

推荐答案

此代码有效,但您需要确保以下几点:
- 文件名实际上存在于您的包中(检查大写/小写),如果不存在,您将不会收到错误消息,但不会显示图片
- 图片尺寸不要太大,不要相互覆盖

This code works, but you need to make sure of a couple of things:
- That the file name actually exists in your bundle (check for uppercase/lowercase), you would not get an error message if it didn't, but it would not show the picture
- That the image sizes are not too large and don't cover each other

这篇关于为数组中的每个项目添加单独的 UIImageViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆