[英]Adding UIImageView array objects to NSMutableArray
我在向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或一個斷點。
我想要這樣做的原因是在后台線程中從Web獲取圖像數組,並在主線程中設置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];
}
如果我不嘗試使其異步,那一切都可以
你為什么不使用
- (void)addObjectsFromArray:(NSArray *)otherArray
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.