UICollectionView存取错误-> UICollectionViewData _setLayoutAttributes:atGlobalIndex: [英] UICollectionView bad acces on -> UICollectionViewData _setLayoutAttributes:atGlobalIndex:

查看:123
本文介绍了UICollectionView存取错误-> UICollectionViewData _setLayoutAttributes:atGlobalIndex:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UICollectionView来显示一批批次为32的图像.每次到达收藏视图的末尾时,我都会加载另一批次的32图像,并调整collectionView contentsize.width的大小以接受新项目.通过使用AFNetworking调用Web服务进行加载.

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.

当我从头到尾以及从头到尾快速滚动时,我会收到EXC_BAD_ACCESS. 当到达CollectionView的末尾时,也会发生这种情况.就像它试图加载一些尚不可用的属性. 自1天以来,我一直试图弄清楚这一点,但没有成功.我尝试使用仪器/启用了NSZombie/guardmalloc ...

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 ...

编辑

还有一个非常奇怪的想法:仅当我用真正的UICollectionView替换了PSTCollectionView时,这种错误的访问才会出现.因此,请确保我只是进行了反向移动并将UICollectionView替换为PSTCollectionView,并且badaccess消失了. 我完全迷路了:)

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

我在项目中同时使用了arc和non arc文件. 我唯一能发现的就是此堆栈跟踪:

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

Blockquote

推荐答案

问题可能是图像太大.您可能想要做的是在每个UIImageView上设置image属性之前,先从下载的图像中调整图像的大小.

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在该主题上发布了非常有用的iBook,其中详细介绍了图像对内存的影响以及如何对其进行优化.这是4.99美元,非常有帮助.

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;
}

现在,如果您正在使用AFNetworking直接使用AFNetworking类别从URL设置图像,则可能需要使用替代方法,以便您可以干预图像并调整其大小.下面的代码可以做到这一点.

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
}];

这篇关于UICollectionView存取错误-> UICollectionViewData _setLayoutAttributes:atGlobalIndex:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆