Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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 CGDataProviderCopyData在内存中累积导致崩溃_Ios_Objective C_Memory Management_Memory Leaks_Cfdata - Fatal编程技术网

Ios CGDataProviderCopyData在内存中累积导致崩溃

Ios CGDataProviderCopyData在内存中累积导致崩溃,ios,objective-c,memory-management,memory-leaks,cfdata,Ios,Objective C,Memory Management,Memory Leaks,Cfdata,好的,我从服务器上下载了一堆大块的ish图像(5mb),然后将这些图像拼接在一起,并从字节数组中呈现整个图像。然而,我已经意识到,每个图像的数据都没有被释放,从而导致内存警告和我的应用程序崩溃。我认为,由于我的显式(uuu bridge_transfer NSData*)转换,ARC将负责释放对象,但事实证明这仍然是一个问题。在仪器中,被称为~1mb的“CGDataProviderCopyData”的对象会累积起来,并且不会因缝合到整个图像中的每个文件而丢弃。有什么想法或者谁能引导我朝正确的方向

好的,我从服务器上下载了一堆大块的ish图像(5mb),然后将这些图像拼接在一起,并从字节数组中呈现整个图像。然而,我已经意识到,每个图像的数据都没有被释放,从而导致内存警告和我的应用程序崩溃。我认为,由于我的显式(uuu bridge_transfer NSData*)转换,ARC将负责释放对象,但事实证明这仍然是一个问题。在仪器中,被称为~1mb的“CGDataProviderCopyData”的对象会累积起来,并且不会因缝合到整个图像中的每个文件而丢弃。有什么想法或者谁能引导我朝正确的方向走?非常感谢

 // 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;
//创建数组以将所有文件添加到总映像中
NSMutableArray*byteArray=[[NSMutableArray alloc]initWithCapacity:(图像高度*图像宽度)];
//遍历文件数组中的每个文件
for(数组中的NSString*文件)
{        
//为单个文件路径设置baseURL
NSString*baseURL=[NSString stringWithFormat:@”http://xx.225.xxx.xxx%@“,[imageInfo objectForKey:@“BaseURL”];
//通过将baseURL附加到文件名来指定imagePath
NSString*imagePath=[NSString stringWithFormat:@“%@%@”,baseURL,文件];
//更改NSString-->NSURL-->NSData
NSURL*imageUrl=[NSURL URLWithString:imagePath];
NSData*imageData=[NSData dataWithContentsOfURL:imageUrl];
//从imageData创建图像
UIImage*image=[UIImage imageWithData:imageData];
CGImageRef cgimage=image.cgimage;
大小\u t宽度=CGImageGetWidth(cgimage);
尺寸高度=CGImageGetHeight(cgimage);
大小\u t bpr=CGImageGetBytesPerRow(cgimage);
大小\u t bpp=CGImageGetBitsPerPixel(cgimage);
大小\u t bpc=CGImageGetBitsPerComponent(cgimage);
每像素大小字节=bpp/bpc;
//从cgimage获取CGDataProviderRef
CGDataProviderRef provider=CGImageGetDataProvider(cgimage);
//这是未被释放的对象
NSData*data=(uuu bridge\u transfer NSData*)CGDataProviderCopyData(provider);//使用(uuu bridge\u transfer NSData*)将提供程序强制转换为键入NSData并将所有权授予ARC,但仍不会放弃
常量UInt8*字节=(字节*)[数据字节];
//记录当前正在迭代的文件
NSLog(@“---将png文件缝合到总图像:%@”,文件);
//用每个像素的通道数据填充字节数组
用于(行大小=0;行<高度;行++)
{
用于(大小\u t列=0;列<宽度;列++)
{
常数单位8*像素=
&字节[行*bpr+列*bytes_/u像素];
for(无符号短i=0;i<4;i+=4)
{
__未使用的无符号短红色=像素[i];//红色通道-未使用
无符号短绿色=像素[i+1];//绿色通道
无符号短蓝色=像素[i+2];//蓝色通道
__未使用的无符号短alpha=像素[i+3];//alpha通道-未使用
//根据强度=[(g*250)+b]创建dicom强度值
无符号短双组分=((绿色*256)+蓝色);
//将无符号短强度值转换为NSNumber,以便可以作为对象存储在数组中
NSNumber*DICOMvalue=[NSNumber NUMBER WITHINT:dicomInt];
//添加到图像阵列(总图像)
[byteArray添加对象:DICOMvalue];
}
}
}
数据=零;
}
乘火车返回;

通过Xcode运行“分析”也不会显示任何明显的泄漏

我几乎一字不差地接受了这段代码,并做了更多的调查。使用CFDataRef/NSData,我能够看到您看到的NSData没有消失的问题,并且我能够通过将使用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;
//创建数组以将所有文件添加到总映像中
NSMutableArray*byteArray=[[NSMutableArray alloc]initWithCapacity:(图像高度*图像宽度)];
//遍历文件数组中的每个文件
for(数组中的NSString*文件)
{        
//为单个文件路径设置baseURL
NSString*baseURL=[NSString stringWithFormat:@”http://xx.225.xxx.xxx%@“,[imageInfo objectForKey:@“BaseURL”];
//通过将baseURL附加到文件名来指定imagePath
NSString*imagePath=[NSString stringWithFormat:@“%@%@”,baseURL,文件];
//更改NSString-->NSURL-->NSData
NSURL*imageUrl=[NSURL URLWithString:imagePath];
NSData*imageData=[NSData dataWithContentsOfURL:imageUrl];
//从imageData创建图像
UIImage*image=[UIImage imageWithData:imageData];
CGImageRef cgimage=image.cgimage;
大小\u t宽度=CGImageGetWidth(cgimage);
尺寸高度=CGImageGetHeight(cgimage);
大小\u t bpr=CGImageGetBytesPerRow(cgimage);
大小\u t bpp=CGImageGetBitsPerPixel(cgimage);
大小\u t bpc=CGImageGetBitsPerComponent(cgimage);
每像素大小字节=bpp/bpc;
//从cgimage获取CGDataProviderRef
CGDataProviderRef provider=CGImageGetDataProvider(cgimage);
@自动释放池
{
//这是未被释放的对象
NSData*data=(uuu bridge\u transfer NSData*)CGDataProviderCopyData(provider);//使用(uuu bridge\u transfer NSData*)将提供程序强制转换为键入NSData并将所有权授予ARC,但仍不会放弃
常量UInt8*字节=(字节*)[数据字节];
//记录当前正在迭代的文件
NSLog(@“---将png文件缝合到总图像:%@”,文件);
//用每个像素的通道数据填充字节数组
用于(行大小=0;行<高度;行++)
{
用于(大小\u t列=0;列<宽度;列++)
{
常数单位8*像素=
&字节[行*bpr+列*bytes_/u像素];
用于(uns)