应用程序在UIScrollView中崩溃,索引超出范围错误

时间:2013-03-05 05:18:53

标签: ios objective-c uiscrollview nsmutablearray

在我的iPhone应用程序中,我使用github的FGallery来显示照片库。 我的问题有时候当我多次点击图库中的图像时(我认为它主要是针对第一个图像),我的应用程序崩溃时出现以下错误消息。

  

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArrayM objectAtIndex:]:索引4294967295超出   界限[0 .. 1]'

我将把数组传递给FGallery类的代码在这里。

在.h

@interface ProdDetails : UIViewController<FGalleryViewControllerDelegate>   {
    NSMutableArray *networkImages;
    FGalleryViewController *networkGallery;
}
@property(strong,nonatomic) NSMutableArray *networkImages;
@property(strong,nonatomic) FGalleryViewController *networkGallery;
-(void)setNetworkImages;
<。> in .m

-(void)setNetworkImages {

    self.networkImages = [[NSMutableArray alloc]init];
    NSArray *data = [prodDet objectForKey:@"model_details"];
    for (id eachDic in data) {

        NSString *eachUrl = [NSString stringWithFormat:@"%@%@",IMG_INIT_URL,[eachDic objectForKey:@"model_imgurl"]];
        eachUrl = [common replaceStringWhiteSpaceinUrl:eachUrl];
        [self.networkImages addObject:eachUrl];
    }
}
-(void)showProductImageGallery    {
    networkGallery = [[FGalleryViewController alloc]initWithPhotoSource:self];

    CATransition *transition = [CATransition animation];
    transition.duration = 1.0;
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromTop;

    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];

    [self.navigationController pushViewController:networkGallery animated:NO];
}

任何人都可以帮我解决这个问题。

修改

感谢All。最后我发现了这个问题。问题出在FGallery。

- (void)scrollingHasEnded {

    _isScrolling = NO;

    NSUInteger newIndex = fabs(floor( _scroller.contentOffset.x / _scroller.frame.size.width ));
    //NSLog(@"%f,%f,%i",_scroller.contentOffset.x,_scroller.frame.size.width,newIndex);
    // don't proceed if the user has been scrolling, but didn't really go anywhere.
    if( newIndex == _currentIndex )
        return;

    // clear previous
    [self unloadFullsizeImageWithIndex:_currentIndex];

    _currentIndex = newIndex;
    [self updateTitle];
    [self updateButtons];
    [self loadFullsizeImageWithIndex:_currentIndex];
    [self preloadThumbnailImages];
}

当我们将第一张图像滚动到上一张图像时,_scroller.contentOffset.x将变为负值。 因此floor( _scroller.contentOffset.x / _scroller.frame.size.width )会产生负面指数。 我刚刚添加了fabs函数来得到一个正数索引

2 个答案:

答案 0 :(得分:0)

错误说明,it because you (or FGallery) trying to access object from networkImages array from the index which is not exist (out of bound)!,使用此代码我们无法帮助您 - 因为它还不够!如果可以'找到FGallery的任何解决方案,您应该关注 MWPhotoBrowser 。我发现它很容易使用。

答案 1 :(得分:0)

使用此行代码

NSMutableArray *data = [prodDet objectForKey:@"model_details"];

而不是

NSArray *data = [prodDet objectForKey:@"model_details"];
相关问题