首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将UIImageView数组对象添加到NSMutableArray

将UIImageView数组对象添加到NSMutableArray
EN

Stack Overflow用户
提问于 2014-07-26 18:17:19
回答 1查看 333关注 0票数 0

我在向UIImageView添加或访问NSMutableArray对象时遇到了困难。

代码语言:javascript
运行
复制
NSMutableArray *imageViewArray = [[NSMutableArray alloc] init];

for(int i=0;i<(imageStringArray.count);i++){
        [imageViewArray addObject:ImgArray[i]];
}

ImgArrayx中填充了来自URL的14幅图像。我要么得到exc_bad_access代码1,要么得到一个断点。

我想这样做的原因是在后台线程中从web获取图像数组,并在主线程中设置UI图像。

编辑:

在另一个注释中,我想这样做的原因是异步地从URL获取和显示图像。

这是我的代码,但我不知道如何将它变成异步方法。

代码语言:javascript
运行
复制
-(void)getSlideshow{

    [self populateStringArray];
    NSString *prefix = @"http://newsongbismarck.com/images/announcements/";
    NSString *suffix = @".jpg";

    //NSUInteger count = [imageStringArray count];
    //get this from the website for now
    int number = (int)[imageStringArray count];
    int PageCount = number;

    //Setup scroll view
    UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 265, 320, 200)];
    Scroller.backgroundColor = [UIColor clearColor];
    Scroller.pagingEnabled = YES;
    Scroller.contentSize = CGSizeMake(PageCount * Scroller.bounds.size.width, Scroller.bounds.size.height);

    //Setup Each View Size
    CGRect ViewSize = Scroller.bounds;


    int x = PageCount;
    int numb = 0;

    UIImageView *ImgArray[PageCount];


    while(x!=0){
        ImgArray[numb] = [[UIImageView alloc] initWithFrame:ViewSize];
        [ImgArray[numb] setImage:[self getSlideshowImages:[[prefix stringByAppendingString:imageStringArray[numb]]stringByAppendingString:suffix]]];
        //[Scroller addSubview:ImgArray[numb]];

        //Offset View Size
        ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);

        x--;
        numb++;
    }

    int integer = (int)[imageStringArray count];
    imageViewArray = [[NSMutableArray alloc] initWithCapacity:integer];

    /*int x2 = PageCount;
    int numb2 = 0;

    while(x2!=0){

        [imageViewArray addObject:ImgArray[numb2]];
        NSLog(@"ImageView added to array");
        x2--;
        numb2++;
    }
*/

    CGRect newViewSize = Scroller.bounds;
    int NumberOfImages = 21;
    int i;
    for(i=0;i<NumberOfImages;i++){

        if(i == 0){
            //Setup first picture
            UIImageView *ImgView = [[UIImageView alloc] initWithFrame:newViewSize];
            NSString *FilePath = [NSString stringWithFormat:@"Page_%d.png", i];
            [ImgView setImage:[UIImage imageNamed:FilePath]];
            [Scroller addSubview:ImgView];
        }else{
            //Setup the rest of the pictures
            newViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
            UIImageView *ImgView = [[UIImageView alloc] initWithFrame:newViewSize];
            NSString *FilePath = [NSString stringWithFormat:@"Page_%d.png", i];
            [ImgView setImage:[UIImage imageNamed:FilePath]];
            [Scroller addSubview:ImgView];
        }
    }

[self.view addSubview:Scroller];

}

如果我不努力让它变成异步的话,一切都会成功的

EN

回答 1

Stack Overflow用户

发布于 2014-07-26 19:42:48

你为什么不用

代码语言:javascript
运行
复制
 - (void)addObjectsFromArray:(NSArray *)otherArray
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24974091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档