【发布时间】:2012-04-20 16:17:43
【问题描述】:
我想从 CGImageRef 获取压缩数据以通过网络连接发送。 为此,我使用 CGImageDestinationRef (CGImageDestinationCreateWithData)。
从 CFDataRef 对象中提取数据后,图像在底部丢失了几行。
这是我所做的(为了阅读目的而清理...):
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);
如果我使用 CFData 对象作为 NSData 对象并将其保存到磁盘,它确实可以工作:
NSData *data = [(NSData *)pngData autorelease];
[data writeToFile:@"/path/test.png" atomically:YES];
但如果是使用 NSDatas 字节方法,它是相同的(截断图像):
NSUInteger len = [data length];
m_image = malloc(len);
memcpy(m_image, (unsigned char*)[data bytes], len);
似乎 CFData 对象中的数据似乎没问题。 但是如何正确获取字节呢?
感谢您的建议。
【问题讨论】:
-
您正在泄漏 CGImageDestinationRef。添加
CFRelease(dataDest)。以防万一这是完整的代码清单:)
标签: macos core-foundation cgimage cfdata