【问题标题】:CFArray to NSData to NSImageCFArray 到 NSData 到 NSImage
【发布时间】:2011-08-11 07:46:48
【问题描述】:

我有一个 plist 文件,其中包含 jpeg 照片数据。我需要把那张照片拿出来。我可以将 plist 文件读入 NSDictionary 并可以通过键“jpegphoto”访问对象。问题是返回的对象是一个我不知道如何使用的 NSCFArray。我可以打印它,我看到一个很好的八位字节流出来了,但这就是我所知道的全部。如何将此 NSCFArray 放入 NSData 或字节数组?

像这样传递给 NSLog 时的 CFArray

NSLog(@"%@", [plistDictionary objectForKey:@"jpegphoto"]);

吐出数百行类似于

5ed15ee6 df44b2ff 00899699 0dd23b58 dccdcadc 6c8d04e1 8b3e1940 af76953e 7c0e1b56 

我知道这是我做的 CFArray b/c

NSLog(@"%@", NSStringFromClass([[data_dictionary objectForKey:@"jpegphoto"] class]));

回来了

NSCFArray

谢谢

【问题讨论】:

  • NSCFArray == NSArray == CFArrayRef.

标签: objective-c xcode macos nsdata nsimage


【解决方案1】:

我能想到的只有这样:

NSArray * array = [ data_dictionary valueForKey:@"jpegphoto" ] ;
NSMutableData * data = [ NSMutableData dataWithCapacity:array.count * 4 ] ;
uint32_t * dataPtr = [ data mutableBytePtr ] ;
NSUInteger index = 0 ;
for( NSNumber * number in array )
{
    dataPtr[ index++ ] = [ number unsignedLongValue ] ;
}

假设数组中充满了 CF/NSNumber 实例。 如果它是原始(非对象)值的 CFArray,请尝试以下操作:

CFArrayRef array = (CFArrayRef)[ data_dictionary valueForKey:@"jpegphoto" ] ;
CFDataRef data = CFDataCreateMutable( kCFAllocatorDefault, CFArrayGetCount( array ) * 4 ) ;
CFArrayGetValues( array, (CFRange){ 0, CFArrayGetCount( array ) }, (void**)CFDataGetMutableBytePtr( data ) ) ;

【讨论】:

  • 您的数据在这两种情况下都在“数据”中。 (一个 NS/CFData)
猜你喜欢
  • 2011-08-16
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 2013-11-17
相关资源
最近更新 更多