Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在arc中调整UIImage大小时内存泄漏_Ios_Iphone_Objective C_Ipad_Uiimage - Fatal编程技术网

Ios 在arc中调整UIImage大小时内存泄漏

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

我正在使用以下方法调整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;
}
UITableViewcellforrowatinexpath方法中,我是这样使用它的

- (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
    还缓存它返回的图像。缓存正在保留图像
  • 如果不释放该图像,缓存将继续保留该图像,直到它释放该图像为止,例如,当出现内存警告时。因此,当您使用ImageName获取图像时,它不会被释放,直到清除缓存

    如果出于任何原因不希望缓存图像,则应使用另一种创建图像的方法,例如,
    -imagewithcontents文件:
    不缓存图像


    您可以预期从
    -imageWithContentsOfFile:
    返回的图像对象将被自动删除而不是缓存,并且它将在运行循环结束时被释放。

    关于
    -imageNamed:
    ,您应该了解两个主要事实,尤其是当您决定使用它时:

  • -imageNamed:
    返回自动删除的图像,该图像将在将来某个时间自动删除
  • -imageNamed
    还缓存它返回的图像。缓存正在保留图像
  • 如果不释放该图像,缓存将继续保留该图像,直到它释放该图像为止,例如,当出现内存警告时。因此,当您使用ImageName获取图像时,它不会被释放,直到清除缓存

    如果出于任何原因不希望缓存图像,则应使用另一种创建图像的方法,例如,
    -imagewithcontents文件:
    不缓存图像


    您可以预期从
    -imageWithContentsOfFile:
    返回的图像对象将自动删除而不是缓存,并将在运行循环结束时解除分配。

    感谢您的解释,我不知道这一点!感谢您的解释@Bhavin我在阅读文档时应该小心,谢谢again@channi:很高兴帮助你!!谢谢你的解释,我不知道!感谢您的解释@Bhavin我在阅读文档时应该小心,谢谢again@channi:很高兴帮助你!!