2
調整的UIImage我調整了UIImages以下方法內存泄漏而在弧
- (UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)newSize forName:(NSString *)name {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[_dictSmallImages setObject:newImage forKey:name];
return newImage;
}
而且在的UITableView的的cellForRowAtIndexPath方法,我用它像這樣
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell.contentView viewWithTag:indexPath.row + 1] removeFromSuperview];
// Configure the cell...
NSString *imageName = [NSString stringWithFormat:@"img%d", indexPath.row + 1];
CGRect imageRect = CGRectMake(8, 5, 304, 190);
UIImage *scaledImage = [_dictSmallImages objectForKey:imageName];
if (![_dictSmallImages objectForKey:imageName]) {
scaledImage = [self resizeImage:[UIImage imageNamed:imageName] toSize:CGSizeMake(304, 190) forName:imageName];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:imageRect];
[imgView setTag:indexPath.row + 1];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:scaledImage];
[cell.contentView addSubview:imgView];
if ((lastIndexPath.row == 10 && indexPath.row == 0) || indexPath.row == lastIndexPath.row) {
if (_delegate) {
NSString *imgName = [NSString stringWithFormat:@"img%d", indexPath.row + 1];
[_delegate selectedText:imgName];
}
[imgView.layer setBorderColor:[UIColor yellowColor].CGColor];
[imgView.layer setBorderWidth:5.0f];
[imgView setAlpha:1.0f];
lastIndexPath = indexPath;
}
return cell;
}
但是當我通過配置文件檢查泄漏時,儀器顯示泄漏如
任何人都可以請讓我知道爲什麼這個泄漏是?
感謝您的解釋,不知道這個! –
感謝您的解釋@Bhavin我在閱讀文檔時應該小心,再次感謝 – channi
@channi:很高興爲您效力! – Bhavin