Gradient images look like very attractive, isn't it?
With this powerful Objective-C method creating an UIImage gradient image is too easy.
You need:
- http://objective-c-functions.blogspot.com/2013/06/helpful-color-struct-rgb-hsv.html
- http://objective-c-functions.blogspot.com/2013/06/get-rgba-components-of-uicolor.html
/** Returns a gradient image.
@param startColor Color to start from.
@param endColor Color to end to.
@param size Size of the result image.
@return Returns a new UIImage which is the result of gradient effect from two colors.
*/
+ (UIImage *)imageGradientWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor size:(CGSize)size {
// Allocate color space.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Allocate bitmap context.
CGContextRef bitmapCotext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
// Create the gradient.
CGFloat locations[2] = { 0.0f, 1.0f };
COLOR_RGBA startRgba = [LMFunctions rgbaFromUIColor:startColor];
COLOR_RGBA endRgba = [LMFunctions rgbaFromUIColor:endColor];
CGFloat components[8] = {
startRgba.r, startRgba.g, startRgba.b, 1.0f, // Start color.
endRgba.r, endRgba.g, endRgba.b, 1.0f }; // End color.
CGGradientRef myGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);
// Draw gradient here.
CGContextDrawLinearGradient(bitmapCotext, myGradient, CGPointMake(0, 0), CGPointMake(size.width, size.height), kCGGradientDrawsAfterEndLocation);
// Create a CGImage from context.
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapCotext);
// Create a UIImage from CGImage.
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
// Release resources.
CGGradientRelease(myGradient);
CGImageRelease(cgImage);
CGContextRelease(bitmapCotext);
CGColorSpaceRelease(colorSpace);
return uiImage;
}
Download the ready-for-use source file (.m) of UIImage gradient from two Colors
A useful and helpful collection library of common Objective-C functions, methods, classes and categories.
Showing posts with label uiimage. Show all posts
Showing posts with label uiimage. Show all posts
June 29, 2013
June 19, 2013
Flip UIButton title and image
Let's say we have a UIButton which can assume two state. With this convenient function we can do a flip animation between two images and two titles.
/** Flip animation for title and image of a button.
The button action does not change. This function changes only the title and the image associated to the button.
@param baseBtn Shared button.
@param baseView A container View. It only serves for animation effect.
@param title1 String of the first title.
@param img1 First image.
@param title2 String of the second title.
@param img2 Second image.
@return Returns true on success, false otherwise.
*/
+ (BOOL)flipButton:(UIButton *)baseBtn ofView:(UIView *)baseView
withTitle1:(NSString *)title1 img1:(UIImage *)img1
withTitle2:(NSString *)title2 img2:(UIImage *)img2 {
NSString *newTitle;
UIImage *newImage;
UIViewAnimationTransition flip;
if ([baseBtn.currentTitle isEqualToString:title1]) {
newTitle = title2;
newImage = img2;
flip = UIViewAnimationTransitionFlipFromRight;
} else if ([baseBtn.currentTitle isEqualToString:title2]) {
newTitle = title1;
newImage = img1;
flip = UIViewAnimationTransitionFlipFromLeft;
} else {
LogError(@"No button found with title: '%@' or '%@'", title1, title2);
return NO;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.50f];
[UIView setAnimationTransition:flip forView:baseView cache:YES];
[baseBtn setTitle:newTitle forState:UIControlStateNormal];
[baseBtn setImage:newImage forState:UIControlStateNormal];
[UIView commitAnimations];
return YES;
}
Download the ready-for-use source file (.m) of Flip UIButton title and image
/** Flip animation for title and image of a button.
The button action does not change. This function changes only the title and the image associated to the button.
@param baseBtn Shared button.
@param baseView A container View. It only serves for animation effect.
@param title1 String of the first title.
@param img1 First image.
@param title2 String of the second title.
@param img2 Second image.
@return Returns true on success, false otherwise.
*/
+ (BOOL)flipButton:(UIButton *)baseBtn ofView:(UIView *)baseView
withTitle1:(NSString *)title1 img1:(UIImage *)img1
withTitle2:(NSString *)title2 img2:(UIImage *)img2 {
NSString *newTitle;
UIImage *newImage;
UIViewAnimationTransition flip;
if ([baseBtn.currentTitle isEqualToString:title1]) {
newTitle = title2;
newImage = img2;
flip = UIViewAnimationTransitionFlipFromRight;
} else if ([baseBtn.currentTitle isEqualToString:title2]) {
newTitle = title1;
newImage = img1;
flip = UIViewAnimationTransitionFlipFromLeft;
} else {
LogError(@"No button found with title: '%@' or '%@'", title1, title2);
return NO;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.50f];
[UIView setAnimationTransition:flip forView:baseView cache:YES];
[baseBtn setTitle:newTitle forState:UIControlStateNormal];
[baseBtn setImage:newImage forState:UIControlStateNormal];
[UIView commitAnimations];
return YES;
}
Download the ready-for-use source file (.m) of Flip UIButton title and image
June 16, 2013
Content mode for scaled, resized or cropped image
How to scale or resize or crop a UIImage image in order to fit in a box? This is my implementation.
/** Returns the appropriate content mode for correctly viewing an image inside a container.
Default behaviour is: if the image is larger (in width) than container then it'll be resized; if it's longer (in height) it'll be cropped at the bottom.
@param imageSize The size of the image.
@param limits The bounds of the container.
@return Returns the right UIViewContentMode value.
*/
+ (UIViewContentMode)contentModeForImageOfSize:(CGSize)imageSize insideBounds:(CGSize)limits {
if (imageSize.width > limits.width) {
return UIViewContentModeScaleAspectFit;
} else {
return UIViewContentModeCenter;
}
}
Download the ready-for-use source file (.m) of Content mode for scaled, resized or cropped image
/** Returns the appropriate content mode for correctly viewing an image inside a container.
Default behaviour is: if the image is larger (in width) than container then it'll be resized; if it's longer (in height) it'll be cropped at the bottom.
@param imageSize The size of the image.
@param limits The bounds of the container.
@return Returns the right UIViewContentMode value.
*/
+ (UIViewContentMode)contentModeForImageOfSize:(CGSize)imageSize insideBounds:(CGSize)limits {
if (imageSize.width > limits.width) {
return UIViewContentModeScaleAspectFit;
} else {
return UIViewContentModeCenter;
}
}
Download the ready-for-use source file (.m) of Content mode for scaled, resized or cropped image
June 14, 2013
ARGB raw data from bitmap
This is not a simple nor straightforward method, because it involves CGImageRef, CGContextRef and raw image data manipulation.
But what it's actually doing is very clear: it takes an image and create its ARGB rappresentation.
/** Create an ARGB Bitmap from image.
@param image CGImageRef of original image.
@return Returns the CGContextRef of a Bitmap representing the image.
*/
+ (CGContextRef)createARGBBitmapContextFromImage:(CGImageRef)image {
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void *bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
// Get image width and height. We'll use the entire image.
size_t pixelsWide = 1;
size_t pixelsHigh = 1;
// Declare the number of bytes per row. Each pixel in the bitmap is represented by 4 bytes; 8 bits each of red, green, blue, and alpha.
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL) {
fprintf(stderr, "Error allocating color space");
return NULL;
}
// Allocate memory for image data. This is the destination in memory where any drawing to the bitmap context will be rendered.
bitmapData = malloc(bitmapByteCount);
if (bitmapData == NULL) {
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease(colorSpace);
return NULL;
}
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits per component.
// Regardless of what the source image format is (CMYK, Grayscale, ...) it will be converted over to the format specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // Bits per component.
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL) {
free (bitmapData);
fprintf (stderr, "Context not created!");
}
// Release colorspace before returning.
CGColorSpaceRelease(colorSpace);
return context;
}
Download the ready-for-use source file (.m) of ARGB raw data from bitmap
But what it's actually doing is very clear: it takes an image and create its ARGB rappresentation.
/** Create an ARGB Bitmap from image.
@param image CGImageRef of original image.
@return Returns the CGContextRef of a Bitmap representing the image.
*/
+ (CGContextRef)createARGBBitmapContextFromImage:(CGImageRef)image {
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void *bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
// Get image width and height. We'll use the entire image.
size_t pixelsWide = 1;
size_t pixelsHigh = 1;
// Declare the number of bytes per row. Each pixel in the bitmap is represented by 4 bytes; 8 bits each of red, green, blue, and alpha.
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL) {
fprintf(stderr, "Error allocating color space");
return NULL;
}
// Allocate memory for image data. This is the destination in memory where any drawing to the bitmap context will be rendered.
bitmapData = malloc(bitmapByteCount);
if (bitmapData == NULL) {
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease(colorSpace);
return NULL;
}
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits per component.
// Regardless of what the source image format is (CMYK, Grayscale, ...) it will be converted over to the format specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // Bits per component.
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL) {
free (bitmapData);
fprintf (stderr, "Context not created!");
}
// Release colorspace before returning.
CGColorSpaceRelease(colorSpace);
return context;
}
Download the ready-for-use source file (.m) of ARGB raw data from bitmap
Subscribe to:
Posts (Atom)