Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gradient Color Image
//
// UIImage+colorImage.h
// HappyMan
//
// Created by Arthur on 2015/6/24.
// Copyright (c) 2015年 LW. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (colorImage)
+ (UIImage *)imageWithColor:(UIColor *)color;
+ (UIImage *)imageVerticalGradientWithSize:(CGSize)size colorset:(NSArray*)colors locations:(CGFloat[])locations;
+ (UIImage *)imageHorizontalGradientWithSize:(CGSize)size colorset:(NSArray*)colors locations:(CGFloat[])locations;
+ (UIImage *)shadowImage;
@end
//
// UIImage+colorImage.m
// HappyMan
//
// Created by Arthur on 2015/6/24.
// Copyright (c) 2015年 LW. All rights reserved.
//
#import "UIImage+colorImage.h"
@implementation UIImage (colorImage)
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)imageVerticalGradientWithSize:(CGSize)size colorset:(NSArray*)colors locations:(CGFloat[])locations
{
UIGraphicsBeginImageContext(size);
// 建立一個 RGB 的顏色空間
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
NSMutableArray *gradientColors = [NSMutableArray array];;
for (UIColor *color in colors) {
if ([color isKindOfClass:[UIColor class]]) {
[gradientColors addObject:(id)color.CGColor];
}
}
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradientColors, locations);
// 建立漸層的起點與終點
CGPoint beginPoint = CGPointMake(size.width / 2, 0);
CGPoint endPoint = CGPointMake(size.width / 2, size.height);
CGContextDrawLinearGradient(context, gradient, beginPoint, endPoint, 0);
CGContextSaveGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 結束 context,並且釋放記憶體
UIGraphicsEndImageContext();
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
return image;
}
+(UIImage *)imageHorizontalGradientWithSize:(CGSize)size colorset:(NSArray *)colors locations:(CGFloat [])locations
{
UIGraphicsBeginImageContext(size);
// 建立一個 RGB 的顏色空間
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
NSMutableArray *gradientColors = [NSMutableArray array];;
for (UIColor *color in colors) {
if ([color isKindOfClass:[UIColor class]]) {
[gradientColors addObject:(id)color.CGColor];
}
}
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradientColors, locations);
// 建立漸層的起點與終點
CGPoint beginPoint = CGPointMake(0, size.height / 2);
CGPoint endPoint = CGPointMake(size.width, size.height / 2);
CGContextDrawLinearGradient(context, gradient, beginPoint, endPoint, 0);
CGContextSaveGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 結束 context,並且釋放記憶體
UIGraphicsEndImageContext();
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
return image;
}
+(UIImage *)shadowImage
{
NSArray *colors = @[[UIColor colorWithWhite:0 alpha:0.4], [UIColor colorWithWhite:0 alpha:0]];
CGFloat locations[] = {0, 1};
return [UIImage imageVerticalGradientWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1) colorset:colors locations:locations];
}
@end
//
// ViewController.h
// HappyTest7
//
// Created by Jason on 2015/9/11.
// Copyright (c) 2015年 HT. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UIImageView *myImageView;
IBOutlet UIImageView *myImageView2;
IBOutlet UIImageView *myImageView3;
}
@end
//
// ViewController.m
// HappyTest7
//
// Created by Jason on 2015/9/11.
// Copyright (c) 2015年 HT. All rights reserved.
//
#import "ViewController.h"
#import "UIImage+colorImage.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = @[[UIColor colorWithWhite:0 alpha:1], [UIColor colorWithWhite:0 alpha:0.8], [UIColor colorWithWhite:0 alpha:0.2], [UIColor colorWithWhite:0 alpha:0.1]];
CGFloat locations[] = {0, 0.2, 0.8, 1};
// 產生垂直漸層影像
UIImage *darkImage = [UIImage imageVerticalGradientWithSize:myImageView3.bounds.size colorset:colors locations:locations];
[myImageView3 setImage:darkImage];
// 產生水平漸層影像
darkImage = [UIImage imageHorizontalGradientWithSize:myImageView2.bounds.size colorset:colors locations:locations];
[myImageView2 setImage:darkImage];
// 產生單純影像
darkImage = [UIImage imageWithColor:[UIColor blueColor]];
[myImageView setImage:darkImage];
}
@end
@happymanx

This comment has been minimized.

Copy link
Owner Author

commented Dec 9, 2015

img_8440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.