mona m in code provided by link you specified contains commented line:
// cgColorSpace = colorSpaceFromPDFArray(colorSpaceArray);
Instead of analyzing colorSpaceArray and getting accurate Color Space a CGColorSpaceCreateDeviceRGB is assigned to cgColorSpace variable which is wrong.
Here is an implementation of missing method, I found it in the internet.
CGColorSpaceRef colorSpaceFromPDFArray(CGPDFArrayRef colorSpaceArray){
CGColorSpaceRef cgColorSpace = NULL, alternateColorSpace = NULL;
CGPDFStreamRef stream;
const char *colorSpaceName = NULL, *alternateColorSpaceName = NULL;
CGPDFInteger numberOfComponents;
CGPDFDictionaryRef dict;
bool retrieved;
CGFloat *range;
CGPDFArrayRef rangeArray;
if (CGPDFArrayGetName(colorSpaceArray, 0, &colorSpaceName)) {
if (strcmp(colorSpaceName, "ICCBased") == 0) {
if (CGPDFArrayGetStream(colorSpaceArray, 1, &stream)) {
dict = CGPDFStreamGetDictionary(stream);
// First obtain the alternate color space if present
if (CGPDFDictionaryGetName(dict, "Alternate", &alternateColorSpaceName)) {
if (strcmp(alternateColorSpaceName, "DeviceRGB") == 0) {
alternateColorSpace = CGColorSpaceCreateDeviceRGB();
} else if (strcmp(alternateColorSpaceName, "DeviceGray") ==
0) {
alternateColorSpace = CGColorSpaceCreateDeviceGray();
} else if (strcmp(alternateColorSpaceName, "DeviceCMYK") ==
0) {
alternateColorSpace = CGColorSpaceCreateDeviceCMYK();
}
}
// Obtain the preferential color space
CGPDFDataFormat dataFormat;
CFDataRef colorSpaceDataPtr =
CGPDFStreamCopyData(stream, &dataFormat);
if (dataFormat == CGPDFDataFormatRaw) {
CGDataProviderRef profile =
CGDataProviderCreateWithCFData(colorSpaceDataPtr);
retrieved = CGPDFDictionaryGetInteger(dict, "N",
&numberOfComponents);
// Deduce an alternate color space if we don't have one
//already
if (alternateColorSpace == NULL) {
switch (numberOfComponents) {
case 1:
alternateColorSpace = CGColorSpaceCreateDeviceGray();
break;
case 3:
alternateColorSpace = CGColorSpaceCreateDeviceRGB();
break;
case 4:
alternateColorSpace = CGColorSpaceCreateDeviceCMYK();
break;
default:
break;
}
}
range = malloc(numberOfComponents * 2 * sizeof(CGFloat));
if (!CGPDFDictionaryGetArray(dict, "Range", &rangeArray)) {
for (int i = 0; i < numberOfComponents * 2; i += 2) {
range[i] = (i % 2 == 0) ? 0.0 : 1.0;
}
} else {
size_t count = CGPDFArrayGetCount(rangeArray);
for (int i = 0; i < count; i++) {
(void)CGPDFArrayGetNumber(rangeArray, i, &range[i]);
}
}
cgColorSpace = CGColorSpaceCreateICCBased(numberOfComponents, range, profile,
alternateColorSpace);
CGDataProviderRelease(profile);
free(range);
if (cgColorSpace) {
// Since we have a preferential color space, we no
//longer need the hang on to the alternate color space
CGColorSpaceRelease(alternateColorSpace);
} else {
cgColorSpace = alternateColorSpace;
}
} else if (dataFormat == CGPDFDataFormatJPEGEncoded) {
//
} else if (dataFormat == CGPDFDataFormatJPEG2000) {
//
}
}
} else if (strcmp(colorSpaceName, "Indexed") == 0) {
CGColorSpaceRef baseSpace;
CGPDFArrayRef base = NULL;
CGPDFInteger highValue = 0;
CGPDFStreamRef stream = NULL;
CGPDFStringRef string;
const unsigned char *chars;
const char *namedColorSpaceName;
if (CGPDFArrayGetArray(colorSpaceArray, 1, &base)) {
baseSpace = colorSpaceFromPDFArray(base);
} else if (CGPDFArrayGetName(colorSpaceArray, 1,
&namedColorSpaceName)) {
if (strcmp(namedColorSpaceName, "DeviceRGB") == 0) {
baseSpace = CGColorSpaceCreateDeviceRGB();
} else if (strcmp(namedColorSpaceName, "DeviceGray") == 0) {
baseSpace = CGColorSpaceCreateDeviceGray();
} else if (strcmp(namedColorSpaceName, "DeviceCMYK") == 0) {
baseSpace = CGColorSpaceCreateDeviceCMYK();
}
}
retrieved = CGPDFArrayGetInteger(colorSpaceArray, 2, &highValue);
if (CGPDFArrayGetStream(colorSpaceArray, 3, &stream)) {
chars = CFDataGetBytePtr(CGPDFStreamCopyData(stream, NULL));
} else if (CGPDFArrayGetString(colorSpaceArray, 3, &string)) {
chars = CGPDFStringGetBytePtr(string);
} else {
// TODO: Raise some error state?
}
cgColorSpace = CGColorSpaceCreateIndexed(baseSpace, highValue,
chars);
}
}
return (CGColorSpaceRef)CFMakeCollectable(cgColorSpace);
}
But unfortunately I am getting some grey dots on image at the end.