iPhone差异处理设备和模拟器之间的图像



我有一个带有透明边框的图像,我正在尝试直接操纵图像像素,遵循此处找到的Apple指南。在设备上运行时,一切正常。但是,当我在模拟器上运行代码时,我发现图像的透明边框慢慢变成黑色,每个调用此功能。奇怪的是,即使我不修改图像数据,透明边框仍会随着对此功能的调用而开始变成黑色。例如,即使我的图像操纵代码调用CGBitmapContextGetData但不使用返回的数据指针,我也会看到相同的问题。为了使问题在模拟器上消失,我必须评论CGBitmapContextGetData的呼叫(当然是数据指针的释放)。示例代码仍在模拟器上修改图像:

+ (UIImage *) updateImage:(UIImage *)inputImage
{
    UIImage *updatedImage;
    /* Update colors in image appropriately */
    CGImageRef image = [inputImage CGImage];
    CGContextRef cgctx = [ColorHandler CreateARGBBitmapContext:image];
    if (cgctx == NULL)
    {
        // error creating context
        NSLog(@"Error creating context.n");
        return nil;
    }
    size_t w = CGImageGetWidth(image);
    size_t h = CGImageGetHeight(image);
    CGRect rect = {{0,0},{w,h}};
    // Draw the image to the bitmap context. Once we draw, the memory
    // allocated for the context for rendering will then contain the
    // raw image data in the specified color space.
    CGContextDrawImage(cgctx, rect, image);
    // Now we can get a pointer to the image data associated with the bitmap
    // context.
    void *data = CGBitmapContextGetData(cgctx);
    CGImageRef ref = CGBitmapContextCreateImage(cgctx);
    updatedImage = [UIImage imageWithCGImage:ref];
    // When finished, release the context
    CGContextRelease(cgctx);
    CGImageRelease(ref);
    // Free image data memory for the context
    if (data)
    {
        free(data);
    }
    return updatedImage;    
}

我在这里阅读了有关如何在设备和模拟器之间对图像进行不同管理的评论和答案,但这并没有帮助我弄清楚我的问题。

我的CreateARGBBitmapContext和示例之间的唯一区别是我称CGColorSpaceCreateDeviceRGB而不是CGColorSpaceCreateWithName,因为我是针对iOS的。该图像的编辑如在iOS设备上运行时所设计的。

我目前正在在主线程中进行所有图像操作以调试此问题。

规格:山狮,Xcode 4.5.2,iOS 6设备,iOS 6模拟器

我能够通过允许石英分配和管理位图(Apple Doc)的内存来解决问题。为此,我更新了CreateARGBBitmapContextCGBitmapContextCreate的调用以通过NULL,然后将所有引用删除到bitmapData

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
// per component. Regardless of what the source image format is 
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (NULL,
                                pixelsWide,
                                pixelsHigh,
                                8,      // bits per component
                                bitmapBytesPerRow,
                                colorSpace,
                                kCGImageAlphaPremultipliedFirst);

然后,在updateImage方法中,我删除了data的释放。现在,它似乎在设备和模拟器上都没有任何问题。

最新更新