0

I am not certain how to debug this error. I know that is happening somewhere in my UIScrollView when I add new data to an array using the function arrayByAddingObjectsFromArray and try to then populate that data to the scrollview.

So it goes like this:

When I scroll to the bottom of the page it calls this function:

-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
    float scrollViewHeight = scrollView.frame.size.height;
    float scrollContentSizeHeight = scrollView.contentSize.height;
    float scrollOffset = scrollView.contentOffset.y;

    if (scrollOffset == 0)
    {
        // then we are at the top
    }
    else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
    {
        [_scrollViewOutlet scrollRectToVisible:CGRectInset([scrollView bounds], 10, 10) animated:NO];

        // then we are at the end
        startingPoint += 36;
        NSLog(@"reached the bottom of the feed");

        if([lastPressedButton isEqualToString:@"top"]){
            [self todaysTopPressed:self];
        }

        if([lastPressedButton isEqualToString:@"latest"]){
            [self latestPressed:self];
        }

    }
}

startingPoint is an integer used in the latest pressed function.

- (IBAction)todaysTopPressed:(id)sender {
    NSString *startPointString = [@(startingPoint) stringValue];

            _loadingImage.hidden = NO;
        [photo getLatestPhotosFromGallery:@"yes" IsLatest:@"no" AtStartingPoint:startPointString AndAmountOfPhotos:@"36" IsPoints:@"yes"];

}

getLatestPhotosFromGallery is in a model which accesses the server and gets a JSON feed of the latest photos and stores it to the UserDefaults then sends a NSNotificationCenter notification to my viewcontroller.

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"FeedNotification"
 object:self];

It successfully runs the NSNotificationCenter and then populates the images:

This is where it crashes:

- (void) populateImages {

    dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
    dispatch_async(myQueue, ^{

    CGFloat contentSizeWidth = 0.0;
    CGSize newSize = _scrollViewOutlet.frame.size;

    NSArray *images_array = [properties objectForKey:@"feedArray"];

        CGRect newFrame = _firstImage.frame;

    for(int i = startingPoint; i < [images_array count]; i++){

            NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:_firstImage];
            UIButton *button = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
            [button addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = i+1;







        NSLog(@"i mod 3: %i", i % 3);

        int xposi = 0;

        if(i % 2 == 0){
            xposi = 0;
        } else {
            xposi = 1;
            newFrame.origin.y += 20+button.frame.size.width;
        }

            newFrame.origin.x = (20+button.frame.size.width) * xposi;

            //contentSizeWidth = (20+button.frame.size.width) * xposi;
            contentSizeWidth = (button.frame.size.width);

            [button setFrame:newFrame];

            if(i < [images_array count]){
                NSDictionary *images_d = [images_array objectAtIndex:i];
                NSString *images_d_image_string = [images_d objectForKey:@"pthumb"];
                NSNumber *images_id = [images_d objectForKey:@"pid"];

                NSString *images_id_string = images_id;

                NSLog(@"image_id: %@", images_id_string);

                NSString *documentsDirectory = [utils getDocumentsDirectoryPath];

                UIImage *image = [utils loadImage:images_id_string ofType:@"jpg" inDirectory:documentsDirectory];

                NSString  *jpgfile = [[utils getDocumentsDirectoryPath] stringByAppendingString:images_id_string];
                jpgfile = [jpgfile stringByAppendingString:@".jpg"];

                NSLog(@"jpgfile: %@", jpgfile);

                BOOL fileExists = NO;
                if([[NSFileManager defaultManager] fileExistsAtPath:jpgfile]){
                    fileExists = YES;
                    NSLog(@"image named %@ exists", images_id_string);
                } else {
                    fileExists = NO;
                    //
                }


                if([[NSFileManager defaultManager] fileExistsAtPath:jpgfile]){
                        dispatch_async(dispatch_get_main_queue(), ^{
                    [[button imageView] setImage:image];
                    [button setImage:image forState:UIControlStateNormal];
                    [button setImage:image forState:UIControlStateSelected];
                    [[button imageView] setContentMode: UIViewContentModeScaleAspectFit];
                        });
                } else {
                    UIImage *imageToSet = [utils getImageFromURL:images_d_image_string];
                        dispatch_async(dispatch_get_main_queue(), ^{
                    [button setImage:imageToSet forState:UIControlStateNormal];
                    [button setImage:imageToSet forState:UIControlStateSelected];
                    [[button imageView] setContentMode: UIViewContentModeScaleAspectFit];
                    [[button imageView] setImage:imageToSet];
                        });
                    [utils saveImage:imageToSet withFileName:images_id_string ofType:@"JPG" inDirectory:[utils getDocumentsDirectoryPath]];
                }

                    dispatch_async(dispatch_get_main_queue(), ^{
                        [button setAlpha:1.0f];
                    });
            } else {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [button setAlpha:0.2f];
                        button.userInteractionEnabled = NO;
                        [button.imageView setContentMode:UIViewContentModeScaleAspectFit];
                //[button sizeToFit];
                    });
            }

            dispatch_async(dispatch_get_main_queue(), ^{
            [_scrollViewOutlet addSubview:button];
            });
                if(i % 4 == 0){
                newSize.height+=20+button.frame.size.width;
                }
    }


        newSize.width = contentSizeWidth;


            dispatch_async(dispatch_get_main_queue(), ^{

                [_scrollViewOutlet setContentSize:newSize];
                _loadingImage.hidden = YES;
            });

    });



}
Julian
  • 9,299
  • 5
  • 48
  • 65
jimbob
  • 3,288
  • 11
  • 45
  • 70
  • What is `_firstImage`? And what is the the intent of that archiver code? That looks highly suspect. – Rob Aug 10 '13 at 15:02
  • You say it crashes on "populateImages", but that function is pretty big. Put some breakpoints in there and find out the exact line that it crashes on. – bachonk Aug 10 '13 at 16:26
  • the feedArray is extended when new data is received from the server: if(startingPoint > 0){ NSMutableArray *combinedArray = [array mutableCopy]; NSMutableArray *oldArray = [[stored_data objectForKey:@"feedArray"] mutableCopy]; [combinedArray arrayByAddingObjectsFromArray:oldArray]; array = combinedArray; NSLog(@"merged array: %@", array); } [stored_data setObject:array forKey:@"feedArray"]; [stored_data synchronize]; – jimbob Aug 10 '13 at 18:11
  • _firstImage is actually a UIButton. I have located the crash down to CGRect newFrame = _firstImage.frame; Not sure why it is on this line? – jimbob Aug 10 '13 at 19:27

0 Answers0