我不确定如何调试此错误。我知道当我使用该函数将新数据添加到数组arrayByAddingObjectsFromArray
并尝试将该数据填充到滚动视图时,我的 UIScrollView 某处发生了这种情况。
所以它是这样的:
当我滚动到页面底部时,它会调用此函数:
-(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
是最新按下的函数中使用的整数。
- (IBAction)todaysTopPressed:(id)sender {
NSString *startPointString = [@(startingPoint) stringValue];
_loadingImage.hidden = NO;
[photo getLatestPhotosFromGallery:@"yes" IsLatest:@"no" AtStartingPoint:startPointString AndAmountOfPhotos:@"36" IsPoints:@"yes"];
}
getLatestPhotosFromGallery 在一个模型中,它访问服务器并获取JSON
最新照片的提要并将其存储到 UserDefaults 然后向NSNotificationCenter
我的viewcontroller
.
[[NSNotificationCenter defaultCenter]
postNotificationName:@"FeedNotification"
object:self];
它成功运行NSNotificationCenter
然后填充图像:
这是它崩溃的地方:
- (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;
});
});
}