1

在使用仪器分析我的应用程序时,我偶然发现了一个特殊的实时字节问题。如果这是任何真正的“优化”或仪器故障,我很感兴趣。如果整体实际内存占用量保持不变,减少“活动字节”有什么好处吗?对于这两种情况,实际内存使用量保持不变。

我在弹出框控制器中使用 UIImagePicker,它会选择一个大(最大 7mb)图像并将其分配给这样的图像视图:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {

    activeImageView.image = img;

}

这是此操作的分配分析。在 2 次图像分配之后,每个图像的活动字节都增加了(大约 5.4 mb)。您可以在图表下方看到图像的 malloc 块(5.4mb 和 768kb)一旦我用新图像(5.mb 到 768kb)替换其中一个图像,实时字节就会“降级”,如图所示大约在 00:55 的图表:

在此处输入图像描述

我正在谈论的特点是,如果获取此图像,将其保存到磁盘,然后我imageWithContentsOfFile:在下面的代码中使用,我的实时字节看起来非常不同:

//this is what I use to save image to disk
     -(NSString* )saveImage:(UIImage*)image withID:(int)imageID 
{
        NSString* filepath = [[self imagesFolderPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.png",imageID]];


        BOOL success =   [UIImagePNGRepresentation(image) writeToFile:filepath atomically:YES];

        if(!success)
        {
            DLog(@"failed to write to file: %@",filepath );
        }

        success = [[NSFileManager defaultManager]fileExistsAtPath:filepath];
        DLog(@"file created: %@",success?@"YES":@"NO");
        return filepath;
    }




//this is the popover delegate telling me when the user has dismissed the image picker in popover
        -(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
        {
                popoverController = nil;

    //take the image from the image view, write it to disk and assign back to the image view
                NSString* imageFilePath = [self saveImage:activeImageView.image withID:self.selectedID];

                if(imageFilePath)
                {
                    //this will work only for an existing icon file, otherwise 
                    imageView.image =[UIImage imageWithContentsOfFile:imageFilePath];
                }
        }

引入此代码后,我的实时字节分配如下图所示。我重复相同的操作(从图像选择器将两个图像分配给 UIImageView,但在这里我添加了一个方法将它们写入磁盘,然后从磁盘读回它们。(三角形斜坡来自当我从图像创建 NSData用于写入磁盘):

在此处输入图像描述

4

0 回答 0