UIImage与UIColor互转

Objective-C

UIColor -> UIImage

?


1

2

3

4

5

6

7

8

9

10

11

- (UIImage*) createImageWithColor: (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 *theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return theImage;

}

UIImage -> UIColor

?


1

[UIColor colorWithPatternImage:[UIImageimageNamed:@"Background"]]

Swift

UIColor -> UIImage

?


1

2

3

4

5

6

7

8

9

10

11

func createImageWithColor(color: UIColor) -> UIImage

{

    let rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f)

    UIGraphicsBeginImageContext(rect.size)

    let context = UIGraphicsGetCurrentContext()

    CGContextSetFillColorWithColor(context, color.CGColor)

    CGContextFillRect(context, rect)

    let theImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return theImage

}

UIImage -> UIColor

?


1

UIColor(PatternImage: UIImage(named: @"Background"))

时间: 2024-10-15 11:27:06

UIImage与UIColor互转的相关文章

iOS中UIImage和UIColor之间的转换

//UIColor 转UIImage - (UIImage*)imageWithColor: (UIColor*)color { CGRect rect=CGRectMake(0.0f, 0.0f, 100.0f, 50.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context,

16进制颜色与UIColor互转

代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIView *view=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; view.backgroundColor=[self colorWithHexString:@"e26562&q

IOS中UIImage和UIColor相互转化

//UIColor 转UIImage - (UIImage*) createImageWithColor: (UIColor*) color { CGRect rect=CGRectMake(0, 0, 1, 1); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color 

iOS开发-UIColor和UIImage之间的转换

UIColor 转UIImage(可将该方法作为一个UIImage的分类) - (UIImage *)imageWithColor:(UIColor *)color { //描述一个矩形 CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); //开启图形上下文 UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); //获得图形上下文 CGContextRef ctx = UIGraphic

UIcolor转UIImage

+ (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0, 0, 1, 1); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFill

UIColor转化为UIImage

+ (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColo

通过UIColor转换为UIImage

+ (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]

根据UIColor对象,返回一张图片UIimage对象

-(UIImage*)createImageWithColor: (UIColor*) color { CGRect rect=CGRectMake(0,0, 1, 1); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFi

iOS 将UIColor转换为UIImage

/** * 将UIColor变换为UIImage * **/+ (UIImage *)createImageWithColor:(UIColor *)color{ CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWit