gpt4 book ai didi

ios - UICollectionView 在 iOS 7 上崩溃

转载 作者:可可西里 更新时间:2023-11-01 06:09:40 25 4
gpt4 key购买 nike

我有一个应用程序可以在 Collection View 中显示来自 Flickr 照片流的最新 50 张图像。它在 iOS 6 上运行良好,但在 iOS 7 上崩溃。如果我卸载该应用程序并重新安装它,它在我第一次加载该 View 时运行良好。当我退出该 View 然后返回时,它崩溃了。有什么想法吗?

这是我得到的错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'too many update animations on one view - limit is 15 in flight at a time (; }; layer = >)'

.h

#import <UIKit/UIKit.h>
#import <dispatch/dispatch.h>
#import "KFBFlickrPhotoManager.h"
#import "KFBImageDetailViewController.h"

@interface KFBFlickrViewController : UIViewController <NSURLConnectionDelegate,NSURLConnectionDataDelegate, UICollectionViewDataSource,UICollectionViewDelegate,FlickrPhotoDelegate, UIGestureRecognizerDelegate>
{

NSMutableArray *imageInfo;
}

@property (retain) NSMutableArray * imageInfo;
@property (retain) KFBFlickrPhotoManager * imageManager;
@property (retain) NSString * html;

@property (nonatomic, retain) NSString *apiKey;
@property (retain, nonatomic) IBOutlet UICollectionView *photoCollectionView;
@property (retain) KFBImageDetailViewController *imageDetailViewController;
@end

.m

#import "KFBFlickrViewController.h"
#import "KFBFlickrPhoto.h"

@interface KFBFlickrViewController ()

@end

@implementation KFBFlickrViewController

@synthesize apiKey=apiKey;
@synthesize photoCollectionView, imageManager, imageInfo,imageDetailViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
apiKey = @"a70fe2e828e66660c74ce0a7c34a55aa";
NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&per_page=50&user_id=52540720@N02&format=json&nojsoncallback=1", apiKey];

NSURL *url = [NSURL URLWithString:urlString];

imageInfo = [NSMutableArray array];

self.imageInfo = [[NSMutableArray alloc] init];
self.imageManager = [[KFBFlickrPhotoManager alloc] initWithURL:url delegate:self];
[imageManager process];


imageInfo = [NSMutableArray array];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUpdated:) name:@"com.KFB.imageupdated" object:nil];
// Custom initialization
}
return self;
}

-(void)viewDidAppear:(BOOL)animated
{
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUpdated:) name:@"com.KFB.imageupdated" object:nil];
}

-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"com.razeware.imagegrabber.imageupdated" object:nil];
}

-(void)showFlickr
{
NSURL *url = [NSURL URLWithString:@"http://www.kyfb.com/photos"];
[[UIApplication sharedApplication] openURL:url];
}

-(void)viewDidLoad
{
[super viewDidLoad];

self.navigationItem.title = @"Photos";
UINib *cellNib = [UINib nibWithNibName:@"CVCell" bundle:nil];
[self.photoCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"photoCell"];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(75, 75)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setSectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
[self.photoCollectionView setCollectionViewLayout:flowLayout];

UIBarButtonItem *openFlickr = [[UIBarButtonItem alloc] initWithTitle:@"More Photos" style:UIBarButtonItemStyleBordered target:self action:@selector(showFlickr)];
self.navigationItem.rightBarButtonItem = openFlickr;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *cellIdentifier = @"photoCell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

KFBFlickrPhoto * info = [imageInfo objectAtIndex:indexPath.row];
UIImageView *imageView = (UIImageView*)[cell viewWithTag:2020];
imageView.image = info.image;

return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
KFBFlickrPhoto *photo = [imageInfo objectAtIndex:indexPath.row];
self.imageDetailViewController = [[KFBImageDetailViewController alloc] initWithNibName:@"KFBImageDetailViewController" bundle:[NSBundle mainBundle]];
imageDetailViewController.photo = photo;
[self.navigationController pushViewController:imageDetailViewController animated:YES];
}


-(void)imagesAvailable:(NSArray *)images done:(BOOL)done {

NSLog(@"Image infos available: %d!", images.count);

NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:images.count];
for(int i = imageInfo.count; i < imageInfo.count + images.count; ++i) {
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:indexPath];
}
[imageInfo addObjectsFromArray:images];
[photoCollectionView insertItemsAtIndexPaths:indexPaths];
//[photoCollectionView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];

}


- (void)imageUpdated:(NSNotification *)notif {

UIImage *info = [notif object];
int row = [imageInfo indexOfObject:info];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];

NSLog(@"Image for row %d updated!", row);
[photoCollectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
//[photoCollectionView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [imageInfo count];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

经过一些研究后,我认为更好的方法是尝试 UICollectionView 的 performBatchUpdates:completion: 方法。您的方法最终看起来像这样:

-(void)imagesAvailable:(NSArray *)images done:(BOOL)done {

[photoCollectionView performBatchUpdates:^{
NSLog(@"Image infos available: %d!", images.count);

NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:images.count];
for(int i = imageInfo.count; i < imageInfo.count + images.count; ++i) {
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:indexPath];
}
[imageInfo addObjectsFromArray:images];
[photoCollectionView insertItemsAtIndexPaths:indexPaths];
} completion:nil];

}

参见 SO question on UICollectionView updates了解更多详情。

关于ios - UICollectionView 在 iOS 7 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19915859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com