Iphone iOS UIImageView为什么仪器显示的带有文件内容的图像的活动字节比UIImagePickerController直接分配的图像少?
在用仪器评测我的应用程序时,我偶然发现了一个特殊的live bytes问题我对这是否是真正的“优化”或仪器故障感兴趣。如果总体实际内存占用保持不变,减少“活动字节”是否有任何好处?两种情况下的实际内存使用量保持不变 我在popover控制器中使用UIImagePicker,它拾取一个大(高达7mb)图像,并将其分配给如下图像视图:Iphone iOS UIImageView为什么仪器显示的带有文件内容的图像的活动字节比UIImagePickerController直接分配的图像少?,iphone,objective-c,ios6,uiimageview,memory-profiling,Iphone,Objective C,Ios6,Uiimageview,Memory Profiling,在用仪器评测我的应用程序时,我偶然发现了一个特殊的live bytes问题我对这是否是真正的“优化”或仪器故障感兴趣。如果总体实际内存占用保持不变,减少“活动字节”是否有任何好处?两种情况下的实际内存使用量保持不变 我在popover控制器中使用UIImagePicker,它拾取一个大(高达7mb)图像,并将其分配给如下图像视图: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingIma
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
activeImageView.image = img;
}
以下是此操作的分配配置文件。分配两个映像后,每个映像的活动字节数都增加了(约5.4 mb)。您可以在图(5.4mb和768kb)下看到图像的malloc块。一旦我用一个新的图像(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];
}
}
在介绍这段代码时,我的live bytes分配如下图所示。我重复相同的操作(从图像选择器将两个图像分配给UIImageView,但这里我添加了一个方法将它们写入磁盘,然后从磁盘读取它们。(三角形渐变来自于我从图像创建NSData以写入磁盘时):