Image I/O framework提供不透明数据类型(opaque data types),从CGImageSourceRef获取图片数据,将图片数据写入到CGImageDestinationRef。它提供一个范围很广的图片格式,包含web格式,动态图,原始相机数据。
在工程中添加Image I/O Framework。然后在需要使用的敌方
#import <ImageIO/ImageIO.h>
即可。
可以通过以下方式来获取支持的图片格式
CFArrayRef mySourceTypes = CGImageSourceCopyTypeIdentifiers();
CFShow(mySourceTypes);
CFArrayRef myDestinationTypes = CGImageDestinationCopyTypeIdentifiers();
CFShow(myDestinationTypes);
一个Image Sources抽象出来了图片数据,通过raw memory buffer减轻开发人员对数据的处理。Image Sources包含不止一个图像,缩略图,各个图像的特征和图片文件。通过CGImageSource实现。
当从Image Sources中创建图片时,可以提供一个index和dictionary(利用键值对)来创建一个缩略图或者是允许缓存。
在创建图片的时候,也需提供一个index值来索引图片,因为Image Sources中可能是多张图片,如果参数时0,那么只有一个图片。可以通过CGImageSourceGetCount来获得图片在Image Sources中的数量。下面是两个例子。
下面是设置了缓存,并使用float-point的方式来编译
CGImageRef MyCreateCGImageFromFile(NSString *path)
{
NSURL *url = [NSURL URLWithString:path];
CGImageRef image;
CGImageSourceRef imageSource;
CFDictionaryRef imageOptions;
CFStringRef imageKeys[2];
CFTypeRef imageValues[2];
//缓存键值对
imageKeys[0] = kCGImageSourceShouldCache;
imageValues[0] = (CFTypeRef)kCFBooleanTrue;
//float-point键值对
imageKeys[1] = kCGImageSourceShouldAllowFloat;
imageValues[1] = (CFTypeRef)kCFBooleanTrue;
//获取Dictionary,用来创建资源
imageOptions = CFDictionaryCreate(NULL, (const void **) imageKeys,
(const void **) imageValues, 2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
//资源创建
imageSource = CGImageSourceCreateWithURL((CFURLRef)url, imageOptions);
CFRelease(imageOptions);
if (imageSource == NULL) {
return NULL;
}
//图片获取,index=0
image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
return image;
}
接下来的例子是设置了缩略图
CGImageRef MyCreateThumbnailCGImageFromURL(NSURL * url, int imageSize)
{
CGImageRef thumbnailImage;
CGImageSourceRef imageSource;
CFDictionaryRef imageOptions;
CFStringRef imageKeys[3];
CFTypeRef imageValues[3];
CFNumberRef thumbnailSize;
//先判断数据是否存在
imageSource = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
if (imageSource == NULL) {
fprintf(stderr, "Image source is NULL.");
return NULL;
}
//创建缩略图等比缩放大小,会根据长宽值比较大的作为imageSize进行缩放
thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize);
imageKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
imageValues[0] = (CFTypeRef)kCFBooleanTrue;
imageKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
imageValues[1] = (CFTypeRef)kCFBooleanTrue;
//缩放键值对
imageKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
imageValues[2] = (CFTypeRef)thumbnailSize;
imageOptions = CFDictionaryCreate(NULL, (const void **) imageKeys,
(const void **) imageValues, 3,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
//获取缩略图
thumbnailImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, imageOptions)
CFRelease(imageOptions);
CFRelease(thumbnailSize);
CFRelease(imageSource);
if (thumbnailImage == NULL) {
return NULL;
return thumbnailImage;
}
当图片从网络中获取的时候,可能由于过大,数据缓慢,这时候就需要渐进式加载图片来显示。主要通过CFData对象来实现:
一个图形目标(Image Destinations)抽象出来了数据写入,并且消除了用户通过原始数据流来处理数据的需求。一个 image destination相当于一张图片或者一组图片,对于每张图片它可以包含缩略图和属性,在通过(URL, CFData,Quartz data consumer)创建CGImageDestination对象之后.可以向里面添加图片数据和设置图片属性。当设置完成之后,调用CGImageDestinationFinalize函数来结束。
给目标图片设置属性
float compression = 1.0; //设置压缩比
int orientation = 4; // 设置朝向bottom, left.
CFStringRef myKeys[3];
CFTypeRef myValues[3];
CFDictionaryRef myOptions = NULL;
myKeys[0] = kCGImagePropertyOrientation;
myValues[0] = CFNumberCreate(NULL, kCFNumberIntType, &orientation);
myKeys[1] = kCGImagePropertyHasAlpha;
myValues[1] = kCFBooleanTrue;
myKeys[2] = kCGImageDestinationLossyCompressionQuality;
myValues[2] = CFNumberCreate(NULL, kCFNumberFloatType, &compression);
myOptions = CFDictionaryCreate( NULL, (const void **)myKeys, (const void **)myValues, 3,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// 记得Release不需要的变量
首先需要创建一个Image Destination,可以通过三种方式CGImageDestinationCreateWithURL, CGImageDestinationCreateWithData, or CGImageDestinationCreateWithDataConsumer(目前不知道URL路径怎么设置才能存入...orz)
- (void) writeCGImage: (CGImageRef) image toURL: (NSURL*) url withType: (CFStringRef) imageType andOptions: (CFDictionaryRef) options
{
CGImageDestinationRef myImageDest = CGImageDestinationCreateWithURL((CFURLRef)url, imageType, 1, nil);
CGImageDestinationAddImage(myImageDest, image, options);//添加数据和图片
CGImageDestinationFinalize(myImageDest);//最后调用,完成数据写入
CFRelease(myImageDest);
}