简体   繁体   English

UICollectionView在iPhone 3GS的iOS 6.0上崩溃

[英]UICollectionView Crashes on iOS 6.0 on iPhone 3GS

The app supposedly crashes while displaying the UICollectionView. 该应用程序在显示UICollectionView时崩溃。 It creates a low memory crash log as well. 它还会创建一个内存不足的崩溃日志。 How can I reduce the memory usage in UICollectionView? 如何减少UICollectionView中的内存使用量? I am using ARC in the project. 我在项目中使用ARC。 Please help...(PS below is my code) 请帮助...(下面的PS是我的代码)

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

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

    static NSString *identifier = @"imageCell";
    imageCell *cvc = (imageCell *)[self.iamgesCollectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if(cvc == nil){
        NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"imageCell" owner:nil options:nil];
        for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass: [imageCell class]]){
                cvc = (imageCell*)currentObject;


                break;
            }
        }
    }
    NSString *path = [untaggedImagesArray objectAtIndex:indexPath.row];
         NSString *path = [untaggedImagesArray objectAtIndex:indexPath.row];
    //[self performSelectorInBackground:@selector(getImage:) withObject:path];

    UIImage *cache_image = [images_cache objectForKey:path];

    if(cache_image)
    {
        [cvc.imageView setImage:cache_image];
    }
    else
    {
        [self performSelectorInBackground:@selector(getImage:) withObject:path];
        //UIImage *img = [UIImage imageWithContentsOfFile:path];
        [images_cache setObject:cellImage forKey:path];
         [cvc.imageView setImage:cellImage];
    }


    if([preTaggedPaths containsObject:path]){
        [cvc.tagLabel setText:[tagStringsDict valueForKey:[NSString stringWithFormat:@"%d",indexPath.row]]];
        [cvc setUserInteractionEnabled:NO];
    }else{
        [cvc.tagLabel setText:@""];
        [cvc setUserInteractionEnabled:YES];
    }

        if([selectedImagesPath containsObject:path]){
            [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_selected.png"]];
        }else{
            [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_unselected.png"]];
        }

//
    return cvc;
    }
}



-(void)getImage:(NSString*)path{
    cellImage = [UIImage imageWithContentsOfFile:path];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"clicked to select at index%d",indexPath.row);
    imageCell *cvc = (imageCell*)[self.iamgesCollectionView cellForItemAtIndexPath:indexPath];
    NSString *pathString = [untaggedImagesArray objectAtIndex:indexPath.row];
    [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_selected.png"]];
    [selectedImagesPath addObject:pathString];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(@"clicked to DEselect at index%d",indexPath.row);
    imageCell *cvc = (imageCell*)[self.iamgesCollectionView cellForItemAtIndexPath:indexPath];
    NSString *pathString = [untaggedImagesArray objectAtIndex:indexPath.row];
    [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_unselected.png"]];
    [selectedImagesPath removeObject:pathString];
//    [cvc setSelected:NO];
}

- --

(void)getImage:(NSString*)path{
    //cellImage = [UIImage imageWithContentsOfFile:path];
    NSLog(@"image path: %@", path);
    cellImage = [UIImage imageNamed:path];

}

Your are saying that the crash is caused by low memory! 您是说崩溃是由内存不足引起的!

A possible problem is your image loading mechanism (What's the size of your images?): 可能的问题是您的图片加载机制(图片的大小是多少?):

[UIImage imageWithContentsOfFile:path];

This will be executed every time for each cell in your collection view. 每次对集合视图中的每个单元格执行一次。

[UIImage imageNamed:@"name"];

for example is caching the image, whereas imageWithContentsOfFile does not! 例如正在缓存图像,而imageWithContentsOfFile则不!

So UIImage's methods imageNamed: and imageWithContentsOfFile do slightly different things. 因此,UIImage的方法imageNamed:imageWithContentsOfFile做一些稍微不同的事情。 imageNamed loads the image in a special system cache, and then future calls with that image path will return the image in the cache instead of reloading it from disk. imageNamed将图像加载到特殊的系统高速缓存中,然后使用该图像路径的将来调用将在高速缓存中返回该图像,而不是从磁盘重新加载它。 imageWithContentsOfFile simply loads the image at the path you specify, but does no caching. imageWithContentsOfFile只是将图像加载到您指定的路径,但不进行缓存。 Multiple calls to imageWithContentsOfFile for the same image will result in multiple copies in memory. 多次调用同一图像的imageWithContentsOfFile将导致内存中有多个副本。

If this is your problem, try caching the images or use a smart way to only load images which are being displayed at the moment. 如果这是您的问题,请尝试缓存图像或使用智能方式仅加载当前正在显示的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM