是否可以在iPhone上使用Core Graphics保存图像?

时间:2010-03-03 11:36:30

标签: iphone core-graphics quartz-2d

以下代码将PDF页面转换为JPEG。它在Snow Leopard上按预期运行:

...
//get the image from the bitmap context
CGImageRef image = CGBitmapContextCreateImage(outContext);

//create the output destination
CGImageDestinationRef outfile = CGImageDestinationCreateWithURL(fileUrl, kUTTypeJPEG, 1, NULL);

//add the image to the output file
CGImageDestinationAddImage(outfile, image, NULL);
CGImageDestinationFinalize(outfile);
...

此代码在为iPhone编译时失败,因为iPhone版本的Core Graphics不包含CGImageDestinationRef。有没有办法使用原生iPhone框架和库将CFImageRef保存到jpeg?我能想到的唯一选择是放弃Core Graphics并使用ImageMagick。

1 个答案:

答案 0 :(得分:9)

UIImage *myImage = [UIImage imageWithCGImage:image];
NSData *jpgData = UIImageJPEGRepresentation(myImage, 0.9f);
[jpgData writeToFile:...