귀하가 충돌이 메모리 부족에 의해 발생되는 말 - 도와주세요 ...
- (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];
}
(. P.S은 아래에있는 내 코드입니다)!
가능한 문제는 (이미지의 크기 무엇입니까?) 이미지 로딩 메커니즘 :
[UIImage imageWithContentsOfFile:path];
이 컬렉션 뷰에서 각 셀마다 실행됩니다.
[UIImage imageNamed:@"name"];
예를 들어 imageWithContentsOfFile은 이미지를 캐싱하지 않습니다.
따라서 UIImage의 방법 imageNamed:
과 imageWithContentsOfFile
은 약간 다른 작업을 수행합니다. imageNamed
은 특수한 시스템 캐시에 이미지를로드하고 그 이미지 경로를 가진 이후의 호출은 디스크에서 이미지를 다시로드하는 대신 캐시의 이미지를 반환합니다. imageWithContentsOfFile
은 지정한 경로의 이미지를 단순히로드하지만 캐싱은 수행하지 않습니다. 동일한 이미지에 대해 imageWithContentsOfFile
을 여러 번 호출하면 메모리에 여러 복사본이 생깁니다.
이것이 문제가되면 이미지를 캐싱하거나 현명한 방법으로 현재 표시되는 이미지 만로드하십시오.
"[UIImage imageWithContentsOfFile : path];"로 이미지를로드하려면 getImage : 메소드를 변경하십시오. 충돌이 멈췄는지 말해봐. – Pochi