0

I am having trouble either adding or accessing UIImageView objects to an NSMutableArray.

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

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

The ImgArray[x] is filled with 14 images from a URL. I either get exc_bad_access code 1 or a break point.

The reason I want to do this is to get an array of images from the web in a background thread and set the UI Images in the main thread.

EDIT:

on a seperate note, the reason I want to do this is to get and display images from a URL asynchronously.

Here is my code, but I don't know how I could make it into an async method.

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

}

It all works if I don't try to make it async

5
  • And you're sure the objects in ImgArray are UIImageView objects? And they have strong references somewhere so they don't go "poof"? Commented Jul 26, 2014 at 18:20
  • When do you get EXC_BAD_ACCESS? Is it during the for loop? Commented Jul 26, 2014 at 18:21
  • 1
    why are you not using the same array, you have imageStringArray and ImgArray thats a little dangerous Commented Jul 26, 2014 at 18:22
  • Are you 100% certain ImgArray truly has 14 objects in them? And are you certain that imageStringArray.count <= ImgArray.count? If imageStringArray.count > ImgArray.count, it will be out of bounds. You should really but in tests like if (ImgArray.count > i) to ensure you don't go out of bounds. You also do need to indicate when you get the EXC_BAD_ACCESSS. Inside our outside of the loop? Commented Jul 26, 2014 at 18:30
  • The exc bad access is within the loop and I am certain that the ImgArray has 14 images. The imagestringarray only holds string values while the ImgArray holds UIImageViews. I'll try all these suggestions and see if it works. Commented Jul 26, 2014 at 19:28

1 Answer 1

0

Why don't you use

 - (void)addObjectsFromArray:(NSArray *)otherArray

?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.