CGImageRefはよく使います.
bitmapレベルから作れて,それからすぐにCIImageが作れてImage Unitで使えるからです.
で,この元になるCGImageをexportしておいて,QCにImage Importerで読み込んで,Kernelを書いてテストして
Image Unitにしたいときがよくあります.(いや無いですね)
というわけで,CGImageRefのexport.
CGImageDestinationRefを使います.
Programming With Quartzに載ってたコードはこちら.
void exportCGImage2PNGFileWithDestination(CGImageRef image,CFURLRef url){
float resolution = 144.0;
CFTypeRef keys[2];
CFTypeRef values[2];
CFDictionaryRef options = NULL;
CGImageDestinationRef imageDestination
= CGImageDestinationCreateWithURL(url,kUTTypePNG,1,NULL);
if(imageDestination == NULL){
return;
}
keys[0] = kCGImagePropertyDPIWidth;
keys[1] = kCGImagePropertyDPIHeight;
values[0] = CFNumberCreate(NULL,kCFNumberFloatType,&resolution);
values[1] = values[0];
options = CFDictionaryCreate(NULL,
keys,
values,
2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFRelease(values[0]);
CGImageDestinationAddImage(imageDestination,image,options);
CFRelease(options);
CGImageDestinationFinalize(imageDestination);
CFRelease(imageDestination);
}
DPIの設定をやっていて丁寧.
CGImageDestinationFinalizeは,書き込み成功かどうかをboolで返すので追加して
DPIの設定なんかをとっぱらって簡単にしちゃうと
BOOL exportCGImage2PNGFileWithDestination(CGImageRef image,CFURLRef url){
CGImageDestinationRef imageDestination
= CGImageDestinationCreateWithURL(url,kUTTypePNG,1,NULL);
if(imageDestination == NULL){
return NO;
}
CGImageDestinationAddImage(imageDestination,image,NULL);
BOOL flag = CGImageDestinationFinalize(imageDestination);
CFRelease(imageDestination);
return flag;
}
こうなります.
CGImageRef *mCGImage; //が作成済みとして
NSURL *url = [NSURL URLWithString:@"file://localhost/Users/hogehoge/hoge.png"];
if(exportCGImage2PNGFileWithDestination(mCGImage,url)){
NSLog(@"success");
}
こういう感じで使います.
引数のCFURLは,NSURLでOK.(toll-free bridge最高)
NSImageの書き出しの時にBUGるとCGImageDestinationがどうのというのが出ますが
裏ではたぶん彼が働いているのだと思われます.
kUTTypePNGを他のに変えるとそのフォーマットで書き出せます.(たぶん)