UICollectionView bad acces on -> UICollectionViewData _setLayoutAttributes:atGlobalIndex:

半城伤御伤魂 提交于 2019-12-23 11:56:40

问题


I use an UICollectionView to display a lot of images with a batch of 32. Every time the i reach the end of the collection view i load an other batch of 32 images and resize the collectionView contentsize.width to accepte the new items. The loading is made by calling a webservice with AFNetworking.

When i scroll very fast from the start to the end and to the end to the start i receive a EXC_BAD_ACCESS. It also happend when a reach the end of the CollectionView. It's like it tries to load some attributes that are not already available. I tried to figure it out since 1 day without any success. I tried with instrument / NSZombie enabled/ guardmalloc ...

EDIT

There is also a very strange think: This bad access only appeared when i replaced PSTCollectionView with the real UICollectionView. So to be sure i just made de inverse move and replace UICollectionView with PSTCollectionView and the badaccess disappeared. I'm totaly lost :)

END EDIT

I'm using both arc and non arc files in the project. The only think i'm able to spot is this stack trace :

Your help will be more than Welcome.

Blockquote


回答1:


The issue may be that the images are just too big. What you may want to do is resize the image from the downloaded image before setting the image property on each UIImageView.

Bill Dudney published a very useful iBook on the topic which goes into detail on the consequences of images on memory and how to optimize for it. It is $4.99 and very helpful.

https://itunes.apple.com/us/book/all-image-io-you-need-to-know/id601759073?mt=11

The following method will resize an image to the given size which will decrease the memory footprint and hopefully prevent your issue.

- (UIImage *)resizeImage:(UIImage *)image size:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    [image drawInRect: CGRectMake(0, 0, width, height)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resizedImage;
}

Now if you are using AFNetworking to set the image directly from the URL using the AFNetworking category you may want to use the alternate method so you can intervene and resize the image. The code below will do that.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:imageURL];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];

[imageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    // resize image
    // set image on imageView
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    // handle error
}];


来源:https://stackoverflow.com/questions/15786084/uicollectionview-bad-acces-on-uicollectionviewdata-setlayoutattributesatglo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!