I want to get the compressed data from a CGImageRef to send it over a network connection. To do so, i use a CGImageDestinationRef (CGImageDestinationCreateWithData).

After pulling the Data out of the CFDataRef Object the image misses a couple of rows at the bottom.

Here is what i do (cleaned for reading purpose...):

CFImageRef image = getTheImageRefIWant();

CFMutableDataRef pngData = CFDataCreateMutable (kCFAllocatorDefault, 0);    

CGImageDestinationRef dataDest = CGImageDestinationCreateWithData (pngData, kUTTypePNG, 1, NULL);

CGImageDestinationAddImage (dataDest, image, NULL); //also tried this with options...

CGImageDestinationFinalize (dataDest);

CFIndex len = CFDataGetLength(pngData);

//copy data
unsigned char* m_image = malloc(len);
CFDataGetBytes (pngData, CFRangeMake(0,len), m_image);

//save data - for testing purpose
FILE *file = fopen("/path/test.png", "wb");
fwrite(m_image, 1, len, file);

//adding header and stuff - skipped here...

//send data
send(sockfd, m_image, len, 0);

If i use the CFData Object as NSData Object and save it to disk it does work:

NSData *data = [(NSData *)pngData autorelease];
[data writeToFile:@"/path/test.png" atomically:YES];

but if is use NSDatas byte method, it is the same (truncated image):

NSUInteger len = [data length];
m_image = malloc(len);
memcpy(m_image, (unsigned char*)[data bytes], len);

seems the data in CFData Object seems to be ok. But how to get the bytes properly?

Thanks for any advice.

有帮助吗?

解决方案

Have you either fflush()ed or fclose()d the file after writing it? Your data may just be buffered in memory.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top