Ios 在arc中调整UIImage大小时内存泄漏
我正在使用以下方法调整UIImages的大小Ios 在arc中调整UIImage大小时内存泄漏,ios,iphone,objective-c,ipad,uiimage,Ios,Iphone,Objective C,Ipad,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
- (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的cellforrowatinexpath方法中,我是这样使用它的
- (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;
}
但当我通过剖面图检查泄漏时,仪器会显示一个类似
有人能告诉我为什么会有这个漏洞吗?关于
-imagename:
,有两个主要事实你应该知道,尤其是当你决定处理它的时候:
-imageNamed:
返回自动删除的图像,该图像将在将来某个时间自动删除-imageNamed
还缓存它返回的图像。缓存正在保留图像-imagewithcontents文件:
不缓存图像
您可以预期从
-imageWithContentsOfFile:
返回的图像对象将被自动删除而不是缓存,并且它将在运行循环结束时被释放。关于-imageNamed:
,您应该了解两个主要事实,尤其是当您决定使用它时:
-imageNamed:
返回自动删除的图像,该图像将在将来某个时间自动删除-imageNamed
还缓存它返回的图像。缓存正在保留图像-imagewithcontents文件:
不缓存图像
您可以预期从
-imageWithContentsOfFile:
返回的图像对象将自动删除而不是缓存,并将在运行循环结束时解除分配。感谢您的解释,我不知道这一点!感谢您的解释@Bhavin我在阅读文档时应该小心,谢谢again@channi:很高兴帮助你!!谢谢你的解释,我不知道!感谢您的解释@Bhavin我在阅读文档时应该小心,谢谢again@channi:很高兴帮助你!!