Thumbnail Image Thumbnail

using sdk 4.1. I get a growing amount of memory, followed by a crash (observed in the Tools) when loading a thumbnail into an image in a table cell. In addition, scrolling is very jerky even with 7-8 cells

    - (UITableViewCell *)tableView:(UITableView *)tableView  
            cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     static NSString *FavouritesCellIdentifier = @"cellIdentifier";


 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 if (cell == nil) 
 {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:cellIdentifier] autorelease];

    UIImageView* imgView =  [[UIImageView alloc] initWithFrame:CGRectMake(10,
                                                                     16, 64, 64)];
    imgView.tag = kImageLabelTag;
    [cell.contentView addSubview:imgView];
    [imgView release];

   }

   UIImageView* imgView = (UIImageView*)[cell viewWithTag:kImageLabelTag];

    NSData *contactImageData = (NSData*)ABPersonCopyImageDataWithFormat(personRef,  
                                                    kABPersonImageFormatThumbnail);
    UIImage *img = [[UIImage alloc] initWithData:contactImageData];
   [imgView setImage:img]; 
   [contactImageData release];
  [img release];

    return cell;
  }

In viewdidunload, I set self.tableview = nil, one way or another, to free up the images stored in the cell as the amount of memory continues to grow even when navigating to a completely different view manager. Memory is only triggered when a view manager is selected that stores this table view.

+3
source share
2 answers

The reason for the failure is that you are releasing an NSData object that you do not need.

, cellForRowAtIndexPath .

, , .

- (UITableViewCell *)tableView:(UITableView *)tableView  
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *FavouritesCellIdentifier = @"cellIdentifier";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:cellIdentifier] autorelease];

    UIImageView* imgView =  [[UIImageView alloc] initWithFrame:CGRectMake(10, 16, 64, 64)];
    imgView.tag = kImageLabelTag;
    [cell.contentView addSubview:imgView];
    [imgView release];

    NSData *contactImageData = (NSData*)ABPersonCopyImageDataWithFormat(personRef, kABPersonImageFormatThumbnail);
    UIImage *img = [[UIImage alloc] initWithData:contactImageData];
    [imgView setImage:img]; 
    [img release];

}

return cell;

}

+1

, , , CFDataRef NSData, . , release , CFDataRef, CFRelease.

:

UIImageView* imgView = (UIImageView*)[cell viewWithTag:kImageLabelTag];

CFDataRef contactImageData = ABPersonCopyImageDataWithFormat(personRef,  
                                                    kABPersonImageFormatThumbnail);
UIImage *img = [[UIImage alloc] initWithData:(NSData*)contactImageData];
[imgView setImage:img]; 
CFRelease(contactImageData);
[img release];
0

Source: https://habr.com/ru/post/1765623/


All Articles