将CFMutableDataRef转换为图像

时间:2013-04-28 19:16:53

标签: iphone ios objective-c ipad

我正在构建一个严重依赖Apples SquareCam示例代码的应用程序。他们通过AVFoundation的自定义相机设置拍照。他们将照片保存到相机胶卷。我想将它存储在文档目录中。它们似乎将图像数据存储在CFMutableDataRef中,但我不确定如何将其转换为图像。有什么想法吗?

Apple的代码:

    CFMutableDataRef destinationData = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef destination = CGImageDestinationCreateWithData(destinationData,
                                                                     CFSTR("public.jpeg"),
                                                                     1,
                                                                     NULL);
BOOL success = (destination != NULL);
require(success, bail);

const float JPEGCompQuality = 0.85f; 
CFMutableDictionaryRef optionsDict = NULL;
CFNumberRef qualityNum = NULL;

qualityNum = CFNumberCreate(0, kCFNumberFloatType, &JPEGCompQuality);
if ( qualityNum ) {
    optionsDict = CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    if ( optionsDict )
        CFDictionarySetValue(optionsDict, kCGImageDestinationLossyCompressionQuality, qualityNum);
    CFRelease( qualityNum );
}

CGImageDestinationAddImage( destination, cgImage, optionsDict );
success = CGImageDestinationFinalize( destination );

if ( optionsDict )
    CFRelease(optionsDict);

require(success, bail);

CFRetain(destinationData);
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library writeImageDataToSavedPhotosAlbum:(id)destinationData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
    if (destinationData)
        CFRelease(destinationData);
}];

[library release];

1 个答案:

答案 0 :(得分:0)

CFMutableDataRef免费桥接到NSMutableData,因此您可以将CFMutableDataRef转换为NSMutableDataNSData(或将其分配给变量该类型)并调用其中一个writeToFile:writeToURL:方法将其存储到文件系统,看看是否会产生图像文件。