添加UIImageView数组对象到NSMutableArray



我有麻烦添加或访问UIImageView对象到NSMutableArray。

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

ImgArray[x]由来自URL的14张图片填充。我要么得到exc_bad_access代码1,要么得到断点。

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

编辑:

另一方面,我想这样做的原因是从URL异步获取和显示图像。

这是我的代码,但是我不知道如何把它变成一个async方法。

-(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];
}

如果我不把它设为async,它就能正常工作

为什么不使用

 - (void)addObjectsFromArray:(NSArray *)otherArray

?

最新更新