【问题标题】:Adding UIImageView array objects to NSMutableArray将 UIImageView 数组对象添加到 NSMutableArray
【发布时间】:2014-07-26 18:17:19
【问题描述】:

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

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 要么得到一个断点。

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

编辑:

另外,我想这样做的原因是异步获取和显示来自 URL 的图像。

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

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

}

如果我不尝试使其异步,一切都会正常

【问题讨论】:

  • 你确定 ImgArray 中的对象是 UIImageView 对象吗?他们在某处有很强的参考价值,所以他们不会“噗嗤”一声?
  • 什么时候获得 EXC_BAD_ACCESS?是在for循环期间吗?
  • 为什么不用同一个数组,你有imageStringArrayImgArray有点危险
  • 您是否 100% 确定 ImgArray 中确实有 14 个对象?你确定 imageStringArray.count ImgArray.count,就会越界。你真的应该在 if (ImgArray.count > i) 这样的测试中确保你不会越界。您还需要指明何时获得 EXC_BAD_ACCESSS。在我们的循环之外?
  • exc 错误访问在循环内,我确信 ImgArray 有 14 个图像。 imagestringarray 只保存字符串值,而 ImgArray 保存 UIImageViews。我会尝试所有这些建议,看看是否可行。

标签: ios objective-c arrays uiimageview


【解决方案1】:

你为什么不使用

 - (void)addObjectsFromArray:(NSArray *)otherArray

?

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    相关资源
    最近更新 更多