gpt4 book ai didi

iphone - UICollectionView 在 iPhone 3GS 上的 iOS 6.0 上崩溃

转载 作者:行者123 更新时间:2023-11-28 20:25:16 25 4
gpt4 key购买 nike

据推测,该应用程序在显示 UICollectionView 时崩溃了。它还会创建低内存崩溃日志。如何减少 UICollectionView 中的内存使用量?我在项目中使用 ARC。请帮助...(附言,下面是我的代码)

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

}

最佳答案

你是说崩溃是内存不足引起的!

一个可能的问题是你的图片加载机制(你的图片有多大?):

[UIImage imageWithContentsOfFile:path];

每次都会为您的 Collection View 中的每个单元格执行此操作。

[UIImage imageNamed:@"name"];

例如正在缓存图像,而 imageWithContentsOfFile 则没有!

所以 UIImage 的方法 imageNamed:imageWithContentsOfFile 做的事情略有不同。 imageNamed 在特殊的系统缓存中加载图像,然后使用该图像路径的 future 调用将返回缓存中的图像,而不是从磁盘重新加载它。 imageWithContentsOfFile 只是在您指定的路径加载图像,但不进行缓存。为同一图像多次调用 imageWithContentsOfFile 将导致内存中有多个副本。

如果这是您的问题,请尝试缓存图像或使用智能方式仅加载当前正在显示的图像。

关于iphone - UICollectionView 在 iPhone 3GS 上的 iOS 6.0 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14278068/

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