AmazeKit 是一个为 iOS 提供图像渲染功能的库。它的目标是在 UIKit 类中使用以 .png
格式保存的图像时保留性能,同时避免在 Photoshop 中创建这些图像,以及将图像捆绑到应用程序中的额外下载大小。图像根据一系列“图像效果”进行渲染,从简单的渐变或圆角到模糊、遮罩和内阴影。AmazeKit 还提供了方便的 UIKit 支持,自动使用正确的图像并在控件大小更改时渲染。自动支持 Retina 显示,并且 AmazeKit 会积极缓存渲染图像以维持最佳性能水平。
获取 AmazeKit 的推荐方法是通过 CocoaPods。
待办事项:添加 AmazeKit 打包、使用静态库、Xcode 子项目和作为 Git 子模块的说明。
要了解 AmazeKit 的工作方式的最佳方式就是看看它实际工作的情况。让我们看看几个常见的用例,并查看它们会产生什么
以下代码设置了一个 AKImageCoordinator
。这个类负责将图像渲染到 UIImageView
中,并自动以适当的大小渲染它
// Noise Effect
AKNoiseImageEffect *noiseEffect =
[[AKNoiseImageEffect alloc] initWithAlpha:0.05f
blendMode:kCGBlendModeMultiply
noiseType:AKNoiseTypeBlackAndWhite];
// Gradient Effect
UIColor *beginColor = [UIColor colorWithRed:144.0f / 255.0f
green:144.0f / 255.0f
blue:144.0f / 255.0f
alpha:1.0f];
UIColor *endColor = [UIColor colorWithRed:103.0f / 255.0f
green:103.0f / 255.0f
blue:103.0f / 255.0f
alpha:1.0f];
AKGradientImageEffect *gradientEffect =
[[AKGradientImageEffect alloc] initWithAlpha:1.0f
blendMode:kCGBlendModeMultiply
colors:@[beginColor, endColor]
direction:AKGradientDirectionVertical
locations:nil];
// Color Effect
AKColorImageEffect *colorEffect =
[[AKColorImageEffect alloc] initWithAlpha:1.0f
blendMode:kCGBlendModeColor
color:[UIColor blueColor]];
// Create the image renderer
AKImageRenderer *imageRenderer = [[AKImageRenderer alloc] init];
[imageRenderer setImageEffects:@[
noiseEffect,
gradientEffect,
colorEffect
]];
AKImageCoordinator *imageCoordinator = [[AKImageCoordinator alloc] init];
[imageCoordinator setImageRenderer:imageRenderer];
[imageCoordinator addImageView:myImageView];
这会产生以下图像输出(对于尺寸为 250 x 100 的 Retina 显示屏上的视图)
AKButtonImageCoordinator
类类似于 AKImageCoordinator
类,但它接受两个图像渲染器而不是一个。“关闭”图像渲染器用于按钮的正常控件状态,“开启”图像渲染器用于按钮的高亮控件状态。AKButtonImageCoordinator
负责渲染按钮的背景图像,并自动将其渲染到正确的大小。以下是一个示例
// The butons will have a noise effect, rounded corners, and a gradient in
// common.
AKNoiseImageEffect *noiseEffect =
[[AKNoiseImageEffect alloc] initWithAlpha:0.05f
blendMode:kCGBlendModeMultiply
noiseType:AKNoiseTypeBlackAndWhite];
UIColor *topColor = [UIColor colorWithRed:144.0f / 255.0f
green:144.0f / 255.0f
blue:144.0f / 255.0f
alpha:1.0f];
UIColor *bottomColor = [UIColor colorWithRed:103.0f / 255.0f
green:103.0f / 255.0f
blue:103.0f / 255.0f
alpha:1.0f];
AKGradientImageEffect *gradientImageEffect =
[[AKGradientImageEffect alloc] initWithAlpha:1.0f
blendMode:kCGBlendModeMultiply
colors:@[topColor, bottomColor]
direction:AKGradientDirectionVertical
locations:nil];
AKCornerRadii cornerRadii = AKCornerRadiiMake(4.0f, 4.0f, 4.0f, 4.0f);
AKCornerRadiusImageEffect *cornerRadiusEffect =
[[AKCornerRadiusImageEffect alloc] initWithAlpha:1.0f
blendMode:kCGBlendModeNormal
cornerRadii:cornerRadii];
// The “off” state is blue, the “on” state is red.
AKColorImageEffect *onColorEffect =
[[AKColorImageEffect alloc] initWithAlpha:1.0f
blendMode:kCGBlendModeColor
color:[UIColor redColor]];
AKColorImageEffect *offColorEffect =
[[AKColorImageEffect alloc] initWithAlpha:1.0f
blendMode:kCGBlendModeColor
color:[UIColor blueColor]];
// We create two image renderers, one for each state
AKImageRenderer *offImageRenderer = [[AKImageRenderer alloc] init];
AKImageRenderer *onImageRenderer = [[AKImageRenderer alloc] init];
// And we assign the image effects for each.
[offImageRenderer setImageEffects:@[
noiseEffect,
gradientImageEffect,
offColorEffect,
cornerRadiusEffect
]];
[onImageRenderer setImageEffects:@[
noiseEffect,
gradientImageEffect,
onColorEffect,
cornerRadiusEffect
]];
// Next we create the button image coordinator and assign the image
// renderers to it
AKButtonImageCoordinator *buttonImageCooordinator =
[[AKButtonImageCoordinator alloc] init];
[buttonImageCooordinator setOffImageRenderer:offImageRenderer];
[buttonImageCooordinator setOnImageRenderer:onImageRenderer];
// Finally, we add a button.
[buttonImageCooordinator addButton:myButton];
这会产生以下图像输出(对于尺寸为 150 x 44 的 Retina 显示屏上的按钮)
在命令行目录中包含两个脚本: gen_docs.sh
和 publish_docs.sh
。这些脚本是为了方便发布appledoc文档而准备的。运行时请小心。
AmazeKit是在底特律由 Jeff Kelley 在 底特律实验室 开发的。