gpt4 book ai didi

ios - CGDataProviderCopyData 在内存中累积导致崩溃

转载 作者:行者123 更新时间:2023-11-29 03:40:47 29 4
gpt4 key购买 nike

好吧,我正在从服务器分段下载一堆大图像 (5mb),然后将这些分段拼接在一起并从字节数组渲染整个图像。但是,我意识到每个图像的数据都没有被释放,因此累积导致内存警告和我的应用程序崩溃。我认为由于我的显式 (__bridge_transfer NSData *) 转换,ARC 会负责释放对象,但它仍然被证明是一个问题。在仪器中,被称为“CGDataProviderCopyData”的约 1mb 的对象会建立起来,并且不会被拼接到整个图像中的每个文件丢弃。任何想法或任何人都可以引导我朝着正确的方向前进?非常感谢。

 // Create  array to add all files into total image
NSMutableArray *byteArray = [[NSMutableArray alloc] initWithCapacity:(imageHeight * imageWidth)];

// Iterate through each file in files array
for (NSString *file in array)
{
// Set baseURL for individual file path
NSString *baseURL = [NSString stringWithFormat:@"http://xx.225.xxx.xxx%@",[imageInfo objectForKey:@"BaseURL"]];

// Specify imagePath by appending baseURL to file name
NSString *imagePath = [NSString stringWithFormat:@"%@%@", baseURL, file];

// Change NSString --> NSURL --> NSData
NSURL *imageUrl = [NSURL URLWithString:imagePath];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];

// Create image from imageData
UIImage *image = [UIImage imageWithData:imageData];
CGImageRef cgimage = image.CGImage;

size_t width = CGImageGetWidth(cgimage);
size_t height = CGImageGetHeight(cgimage);

size_t bpr = CGImageGetBytesPerRow(cgimage);
size_t bpp = CGImageGetBitsPerPixel(cgimage);
size_t bpc = CGImageGetBitsPerComponent(cgimage);
size_t bytes_per_pixel = bpp / bpc;

// Get CGDataProviderRef from cgimage
CGDataProviderRef provider = CGImageGetDataProvider(cgimage);

// This is the object that is not being released
NSData *data = (__bridge_transfer NSData *)CGDataProviderCopyData(provider); //Using (__bridge_transfer NSData *) casts the provider to type NSData and gives ownership to ARC, but still not discarded

const UInt8 *bytes = (Byte *)[data bytes];

// Log which file is currently being iterated through
NSLog(@"---Stitching png file to total image: %@", file);

// Populate byte array with channel data from each pixel
for(size_t row = 0; row < height; row++)
{
for(size_t col = 0; col < width; col++)
{
const UInt8* pixel =
&bytes[row * bpr + col * bytes_per_pixel];

for(unsigned short i = 0; i < 4; i+=4)
{
__unused unsigned short red = pixel[i]; // red channel - unused
unsigned short green = pixel[i+1]; // green channel
unsigned short blue = pixel[i+2]; // blue channel
__unused unsigned short alpha = pixel[i+3]; // alpha channel - unused

// Create dicom intensity value from intensity = [(g *250) + b]
unsigned short dicomInt = ((green * 256) + blue);

//Convert unsigned short intensity value to NSNumber so can store in array as object
NSNumber *DICOMvalue = [NSNumber numberWithInt:dicomInt];

// Add to image array (total image)
[byteArray addObject:DICOMvalue];
}
}
}
data = nil;
}
return byteArray;

通过 Xcode 运行“分析”也没有显示任何明显的泄漏。

最佳答案

我几乎逐字逐句地获取了这段代码,并做了一些更多的调查。通过 CFDataRef/NSData,我能够看到您在 NSDatas 中遇到的问题并没有消失,并且我能够通过将使用 NSData 的代码部分包装在 @autoreleasepool 中来解决它。范围,如下所示:

 // Create  array to add all files into total image
NSMutableArray *byteArray = [[NSMutableArray alloc] initWithCapacity:(imageHeight * imageWidth)];

// Iterate through each file in files array
for (NSString *file in array)
{
// Set baseURL for individual file path
NSString *baseURL = [NSString stringWithFormat:@"http://xx.225.xxx.xxx%@",[imageInfo objectForKey:@"BaseURL"]];

// Specify imagePath by appending baseURL to file name
NSString *imagePath = [NSString stringWithFormat:@"%@%@", baseURL, file];

// Change NSString --> NSURL --> NSData
NSURL *imageUrl = [NSURL URLWithString:imagePath];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];

// Create image from imageData
UIImage *image = [UIImage imageWithData:imageData];
CGImageRef cgimage = image.CGImage;

size_t width = CGImageGetWidth(cgimage);
size_t height = CGImageGetHeight(cgimage);

size_t bpr = CGImageGetBytesPerRow(cgimage);
size_t bpp = CGImageGetBitsPerPixel(cgimage);
size_t bpc = CGImageGetBitsPerComponent(cgimage);
size_t bytes_per_pixel = bpp / bpc;

// Get CGDataProviderRef from cgimage
CGDataProviderRef provider = CGImageGetDataProvider(cgimage);

@autoreleasepool
{
// This is the object that is not being released
NSData *data = (__bridge_transfer NSData *)CGDataProviderCopyData(provider); //Using (__bridge_transfer NSData *) casts the provider to type NSData and gives ownership to ARC, but still not discarded

const UInt8 *bytes = (Byte *)[data bytes];

// Log which file is currently being iterated through
NSLog(@"---Stitching png file to total image: %@", file);

// Populate byte array with channel data from each pixel
for(size_t row = 0; row < height; row++)
{
for(size_t col = 0; col < width; col++)
{
const UInt8* pixel =
&bytes[row * bpr + col * bytes_per_pixel];

for(unsigned short i = 0; i < 4; i+=4)
{
__unused unsigned short red = pixel[i]; // red channel - unused
unsigned short green = pixel[i+1]; // green channel
unsigned short blue = pixel[i+2]; // blue channel
__unused unsigned short alpha = pixel[i+3]; // alpha channel - unused

// Create dicom intensity value from intensity = [(g *250) + b]
unsigned short dicomInt = ((green * 256) + blue);

//Convert unsigned short intensity value to NSNumber so can store in array as object
NSNumber *DICOMvalue = [NSNumber numberWithInt:dicomInt];

// Add to image array (total image)
[byteArray addObject:DICOMvalue];
}
}
}
data = nil;
}
}
return byteArray;

添加 @autoreleasepool 后,然后我注释掉了创建 NSNumbers 并将它们放入数组的部分,并且我能够在 Instruments 的 Allocations 模板中看到,确实 CFData 对象现在在每一轮中都被释放循环。

我注释掉创建 NSNumbers 并将它们放入数组的部分的原因是,使用该代码,您最终将添加 width * height * 4 NS 编号为 byteArray .这意味着即使 NSData 被正确释放,您的堆使用量也会增加 width * height * 4 * <at least 4 bytes, maybe more>无论。也许这就是您需要做的,但这确实让我更难了解 NSData 发生了什么,因为它们的大小与 NSNumber 数组相比显得相形见绌。

希望对您有所帮助。

关于ios - CGDataProviderCopyData 在内存中累积导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18496593/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com