2012-04-20 30 views
0

我想從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的數據似乎沒問題。 但如何正確獲取字節?

感謝您的任何建議。

+0

您正在泄漏CGImageDestinationRef。添加一個'CFRelease(dataDest)'。以防萬一這是完整的代碼列表:) – Buzzy 2013-05-31 11:29:41

回答

0

你有沒有fflush() ed或fclose() d在寫完文件後?您的數據可能只是緩衝在內存中。

+0

哦,我的...馬虎我。謝謝你看看!現在一切正常。 – inx 2012-04-23 12:02:03