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
imageStringArray
andImgArray
thats a little dangerous