【问题标题】:compressed CGImage with CGImageDestination truncated after getting bytes压缩的 CGImage 与 CGImageDestination 在获取字节后被截断
【发布时间】: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


【解决方案1】:

你有没有fflush()ed 或fclose()d 写完文件?您的数据可能只是在内存中缓冲。

【讨论】:

  • 哦,我的...马虎我。谢谢你看!现在一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
相关资源
最近更新 更多