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, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage *Image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return Image;

}

 

 

 

  //UIImage转UIColor

    UIButton *Button = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 100,50)];

    UIImage *image = [UIImage imageNamed:@"radio_bn.png"];   

 

    [cancelButton setBackgroundColor:[UIColor colorWithPatternImage:image]];

 

posted @ 2015-12-31 11:11  GX-  阅读(398)  评论(0编辑  收藏  举报