How to get a color image in iPhone sdk

后端 未结 2 1208
情深已故
情深已故 2020-12-10 07:29

I want something as follows

UIImage *solid = [UIImage imageWithColor:[UIColor darkGrayColor]];

to create an image with respect to some colo

2条回答
  •  情深已故
    2020-12-10 07:57

    You can draw the color into a CGContext and then capture an image from it:

    - (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size {
      //Create a context of the appropriate size
      UIGraphicsBeginImageContext(size);
      CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
      //Build a rect of appropriate size at origin 0,0
      CGRect fillRect = CGRectMake(0,0,size.width,size.height);
    
      //Set the fill color
      CGContextSetFillColorWithColor(currentContext, color.CGColor);
    
      //Fill the color
      CGContextFillRect(currentContext, fillRect);
    
      //Snap the picture and close the context
      UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(void);
      UIGraphicsEndImageContext();
    
      return retval;
    }
    

提交回复
热议问题