我想從CGImageRef獲取壓縮數據並通過網絡連接發送。 爲此,我使用CGImageDestinationRef(CGImageDestinationCreateWithData)。獲取字節後CGImageDestination被壓縮的CGImage被截斷
將數據拉出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 Object的數據似乎沒問題。 但如何正確獲取字節?
感謝您的任何建議。
您正在泄漏CGImageDestinationRef。添加一個'CFRelease(dataDest)'。以防萬一這是完整的代碼列表:) – Buzzy 2013-05-31 11:29:41