框架的介绍: The Image I/O 编程接口允许应用读写大部分的图片格式资源. 起初是CoreGraphics的一部分,除了在Image I/O Framework中使用,也可以在CoreGraphics中访问. Image I/O 提供了有效的方式访问图片资源由于它的高效,便于链接元数据,提供颜色管理.
框架的核心部分: Basics of Using Image I/O 、 Creating and Using Image Sources、Working with Image Destinations
1. Basics of Using Image I/O
它支持大量的图片格式, 包含标准的web格式, 动图和未处理的相机数据.除此以外,还有一下特性.
在Mac平台下快速的图片编码
增量的图片加载
支持图片元数据
有效的缓存
支持的图片格式
获取并打印支持的UTIs
CFArrayRef mySourceTypes = CGImageSourceCopyTypeIdentifiers();
CFShow(mySourceTypes);
CFArrayRef myDestinationTypes = CGImageDestinationCopyTypeIdentifiers();
CFShow(myDestinationTypes);
2.Creating and Using Image Sources
1>. Creating an Image from an Image Source
CGImageRef MyCreateCGImageFromFile (NSString* path){ // Get the URL for the pathname passed to the function. NSURL *url = [NSURL fileURLWithPath:path]; CGImageRef myImage = NULL; CGImageSourceRef myImageSource; CFDictionaryRef myOptions = NULL; CFStringRef myKeys[2]; CFTypeRef myValues[2]; // Set up options if you want them. The options here are for // caching the image in a decoded form and for using floating-point // values if the image format supports them. myKeys[0] = kCGImageSourceShouldCache; myValues[0] = (CFTypeRef)kCFBooleanTrue; myKeys[1] = kCGImageSourceShouldAllowFloat; myValues[1] = (CFTypeRef)kCFBooleanTrue; // Create the dictionary myOptions = CFDictionaryCreate(NULL, (const void **) myKeys, (const void **) myValues, 2, &kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks); // Create an image source from the URL. myImageSource = CGImageSourceCreateWithURL((CFURLRef)url, myOptions); CFRelease(myOptions); // Make sure the image source exists before continuing if (myImageSource == NULL){ fprintf(stderr, "Image source is NULL."); return NULL; } // Create an image from the first item in the image source. myImage = CGImageSourceCreateImageAtIndex(myImageSource, 0, NULL); CFRelease(myImageSource); // Make sure the image exists before continuing if (myImage == NULL){ fprintf(stderr, "Image not created from image source."); return NULL; } return myImage;}
2>.Creating a Thumbnail Image from an Image Source
CGImageRef MyCreateThumbnailImageFromData (NSData * data, int imageSize){ CGImageRef myThumbnailImage = NULL; CGImageSourceRef myImageSource; CFDictionaryRef myOptions = NULL; CFStringRef myKeys[3]; CFTypeRef myValues[3]; CFNumberRef thumbnailSize; // Create an image source from NSData; no options. myImageSource = CGImageSourceCreateWithData((CFDataRef)data, NULL); // Make sure the image source exists before continuing. if (myImageSource == NULL){ fprintf(stderr, "Image source is NULL."); return NULL; } // Package the integer as a CFNumber object. Using CFTypes allows you // to more easily create the options dictionary later. thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize); // Set up the thumbnail options. myKeys[0] = kCGImageSourceCreateThumbnailWithTransform; myValues[0] = (CFTypeRef)kCFBooleanTrue; myKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent; myValues[1] = (CFTypeRef)kCFBooleanTrue; myKeys[2] = kCGImageSourceThumbnailMaxPixelSize; myValues[2] = (CFTypeRef)thumbnailSize; myOptions = CFDictionaryCreate(NULL, (const void **) myKeys, (const void **) myValues, 2, &kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks); // Create the thumbnail image using the specified options. myThumbnailImage = CGImageSourceCreateThumbnailAtIndex(myImageSource, 0, myOptions); // Release the options dictionary and the image source // when you no longer need them. CFRelease(thumbnailSize); CFRelease(myOptions); CFRelease(myImageSource); // Make sure the thumbnail image exists before continuing. if (myThumbnailImage == NULL){ fprintf(stderr, "Thumbnail image not created from image source."); return NULL; } return myThumbnailImage;}
3.Working with Image Destinations
Setting the Properties of an Image Destination
float compression = 1.0; // Lossless compression if available.
int orientation = 4; // Origin is at 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 the CFNumber and CFDictionary objects when you no longer need them.
Writing an Image to an Image Destination
- (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);}
Creating an Animated Image
letloopCount = 1
letframeCount = 60
varfileProperties = NSMutableDictionary()
fileProperties.setObject(kCGImagePropertyPNGDictionary, forKey: NSDictionary(dictionary: [kCGImagePropertyAPNGLoopCount: frameCount]))
varframeProperties = NSMutableDictionary()
frameProperties.setObject(kCGImagePropertyPNGDictionary, forKey: NSDictionary(dictionary: [kCGImagePropertyAPNGDelayTime: 1.0 / Double(frameCount)]))
guardletdestination = CGImageDestinationCreateWithURL(fileURL, kUTTypePNG, frameCount,nil)else{
// Provide error handling here.
}
CGImageDestinationSetProperties(destination, fileProperties.copy()as? NSDictionary)
foriin0..
autoreleasepool {
letradians = M_PI * 2.0 * Double(i) / Double(frameCount)
guardletimage = imageForFrame(size: CGSize(width: 300, height: 300))else{
return
}
CGImageDestinationAddImage(destination, image, frameProperties)
}
}
if!CGImageDestinationFinalize(destination) {
// Provide error handling here.
}
网友评论