【问题标题】:CGDataProvider works the first time, returns an empty image the second timeCGDataProvider 第一次工作,第二次返回一个空图像
【发布时间】:2012-12-17 21:08:59
【问题描述】:

我正在尝试从我的 ios 应用程序中的 png 图像资产中读取 ARGB 像素数据。 我正在使用 CGDataProvider 来获取 CFDataRef,如下所述:

http://developer.apple.com/library/ios/#qa/qa1509/_index.html

当我第一次在某个图像上使用它时,它可以完美运行。但我第二次在 THE SAME 图像上使用它时,它返回长度为 0 的 CFDataRef。

也许我没有发布一些东西?为什么要这么做?

- (GLuint)initWithCGImage:(CGImageRef)newImageSource
{
    CGDataProviderRef dataProvider;
    CFDataRef dataRef;

    GLuint t;

    @try {
 //   NSLog(@"initWithCGImage");
 //   report_memory2();
    CGFloat widthOfImage = CGImageGetWidth(newImageSource);
    CGFloat heightOfImage = CGImageGetHeight(newImageSource);
    //    pixelSizeOfImage = CGSizeMake(widthOfImage, heightOfImage);
    //    CGSize pixelSizeToUseForTexture = pixelSizeOfImage;

    //    CGSize scaledImageSizeToFitOnGPU = [GPUImageOpenGLESContext sizeThatFitsWithinATextureForSize:pixelSizeOfImage];

    GLubyte *imageData = NULL;
    //CFDataRef dataFromImageDataProvider;


   // stbi stbiClass;
    int x;
    int y;
    int comp;

    dataProvider = CGImageGetDataProvider(newImageSource);
    dataRef = CGDataProviderCopyData(dataProvider);

    const unsigned char * bytesRef = CFDataGetBytePtr(dataRef);
   // NSUInteger length = CFDataGetLength(dataRef);

    //CGDataProviderRelease(dataProvider);
    //dataProvider = nil;
    /*
    UIImage *tmpImage = [UIImage imageWithCGImage:newImageSource];

    NSData *data2 =         UIImagePNGRepresentation(tmpImage);
//    if (data2==NULL)
//        data2 =        UIImageJPEGRepresentation(tmpImage, 1);

    unsigned char *bytes = (unsigned char *)[data2 bytes];
    NSUInteger length = [data2 length];*/
//    stbiClass.img_buffer = bytes;
//    stbiClass.buflen = length;
//    stbiClass.img_buffer_original = bytes;
//    stbiClass.img_buffer_end = bytes + length;

//    unsigned char *data = stbi_load_main(&stbiClass, &x, &y, &comp, 0);
    //unsigned char * data = bytesRef;
    x = widthOfImage;
    y = heightOfImage;
    comp = CGImageGetBitsPerPixel(newImageSource)/8;

    int textureWidth = [self CalcPow2: x];
    int textureHeight = [self CalcPow2: y];

    unsigned char *scaledData = [self scaleImageWithParams:@{@"x":@(x), @"y":@(y), @"comp":@(comp), @"targetX":@(textureWidth), @"targetY":@(textureHeight)} andData:(unsigned char *)bytesRef];
    //CFRelease (dataRef);
   // dataRef = nil;
 //    free (data);

    glGenTextures(1, &t);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, t);
    GLint format  = (comp > 3) ? GL_RGBA : GL_RGB;


    imageData = scaledData;

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, format, textureWidth, textureHeight, 0, format, GL_UNSIGNED_BYTE, imageData);
    //GLenum err = glGetError();
    }
    @finally
    {
        CGDataProviderRelease(dataProvider);
//        CGColorSpaceRelease(colorSpaceRef);
        CGImageRelease(dataRef);
    }


    return t;
}

第二次调用来自 [UIimage imageNamed: Path] 的 CGImageRef 与第一次具有相同的路径时,我得到一个长度为 0 的 dataRef。 不过第一次就可以了。

【问题讨论】:

  • 一些代码会有很大帮助。
  • @H2CO3:这似乎是关于创建数据提供者,然后从中创建图像,而不是创建图像并获取其数据提供者。还是我错过了什么?
  • @PeterHosey 不,但我认为这可能与这个问题有关。

标签: objective-c ios core-foundation


【解决方案1】:

我发现我发布并修复的代码存在一个大问题。 首先,即使我没有两次加载相同的图像,而是加载更多图像,我也会崩溃。由于这个问题与内存有关,它以各种奇怪的方式失败了。

代码的问题是我正在调用:“CGDataProviderRelease(dataProvider);” 我正在使用 newImageSource 的数据提供者,但我没有创建这个数据提供者。这就是为什么我不应该释放它。 只有当你创建、保留或复制它们时,你才需要发布它们。

除此之外,由于内存不足,我的应用程序有时会崩溃,但在修复此问题后,我能够使用我尽快分配和释放的“经济”类型。

目前我看不出这个特定代码有什么问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多