UIImageColorPalette
is a versatile utility for extracting the prominent colors from images in iOS. It efficiently identifies and provides the three most prevalent colors in a UIImage
.
To install UIImageColorPalette
, follow these steps:
-
Download: Download the
UIImageColorPalette.h
andUIImageColorPalette.m
files. -
Add to project: Integrate the downloaded files into your project.
-
Import the Class: Import the class wherever you want to use it:
#import "UIImageColorPalette.h"
Here are quick examples of how to use UIImageColorPalette
to extract the color palette from an image:
// Load an image
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
// Retrieve the color palette
UIImageColorPalette *palette = [image retrieveColorPaletteWithQuality:UIImageResizeQualityStandard];
if (palette) {
NSLog(@"Color Palette: %@", palette);
} else {
NSLog(@"Failed to retrieve color palette.");
}
// Set the background color of view
UIColor *backgroundColor = palette.primary;
self.view.backgroundColor = backgroundColor;
// Set the text color for a label
UIColor *textColor = palette.secondary;
myLabel.textColor = textColor;
// Load an image from a remote URL
NSURL *imageURL = [NSURL URLWithString:@"https://example.com/image.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
// Retrieve the color palette
UIImageColorPalette *palette = [image retrieveColorPaletteWithQuality:UIImageResizeQualityStandard];
if (palette) {
NSLog(@"Color Palette: %@", palette);
} else {
NSLog(@"Failed to retrieve color palette.");
}
// Load an image from a remote URL asynchronously
NSURL *imageURL = [NSURL URLWithString:@"https://example.com/image.jpg"];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
[image retrieveColorPaletteWithQuality:UIImageResizeQualityStandard completion:^(UIImageColorPalette *palette) {
if (palette) {
NSLog(@"Color Palette: %@", palette);
} else {
NSLog(@"Failed to retrieve color palette.");
}
}];
});
You can use other presets besides UIImageResizeQualityStandard
by using the following options:
UIImageResizeQualityLow
: Use low-quality resizing algorithm.UIImageResizeQualityMedium
: Use medium-quality resizing algorithm.UIImageResizeQualityHigh
: Use high-quality resizing algorithm.
// Load an image
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
// Retrieve the color palette with custom resize quality
UIImageColorPalette *palette = [image retrieveColorPaletteWithQuality:UIImageResizeQualityHigh];
if (palette) {
NSLog(@"Color Palette: %@", palette);
} else {
NSLog(@"Failed to retrieve color palette.");
}
This project is licensed under the MIT License.