iOS implements the code that takes the color from the background image

  • 2020-05-19 05:58:55
  • OfStack

The example of this article explains the code of iOS to take color from the background image, which is Shared with you for your reference. The details are as follows

Implementation code:

void *bitmapData; // A pointer to a memory space whose size is equal to the image used RGB The number of bytes occupied by a channel. 
static CGContextRef CreateRGBABitmapContext (CGImageRef inImage)
  CGContextRef context = NULL;
  CGColorSpaceRef colorSpace;
  int bitmapByteCount;
  int bitmapBytesPerRow;
  size_t pixelsWide = CGImageGetWidth(inImage); // Gets the number of pixels in the landscape 
  size_t pixelsHigh = CGImageGetHeight(inImage);
  bitmapBytesPerRow  = (pixelsWide * 4); // every 1 The number of bytes occupied by each pixel in a row ARGB4 Each channel is occupied 8 a bit(0-255) The space of 
  bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); // Calculate the number of bytes taken by the entire graph 
  colorSpace = CGColorSpaceCreateDeviceRGB();// Create device-dependent RGB channel 
  // Allocate enough memory space to hold the number of image bytes 
  bitmapData = malloc( bitmapByteCount );
  // create CoreGraphic The graphic context that describes bitmaData Points to the memory space needed to draw the image 1 Some drawing parameters 
  context = CGBitmapContextCreate (bitmapData,
  //Core Foundation By containing Create , Alloc The method name creates a pointer that needs to be used CFRelease() Function to release 
  CGColorSpaceRelease( colorSpace );
  return context;
//  return 1 The pointer to 1 Each of the arrays 4 All the elements are on the image 1 In pixels RGBA The numerical (0-255) , with no sign char Because it's in the right range 0-255
static unsigned char *RequestImagePixelData(UIImage *inImage)
  CGImageRef img = [inImage CGImage];
  CGSize size = [inImage size];
  // Use the above function to create the context 
  CGContextRef cgctx = CreateRGBABitmapContext(img);
  CGRect rect = {{0,0},{size.width, size.height}};
  // Draws the target image to the specified context, actually within the context bitmapData . 
  CGContextDrawImage(cgctx, rect, img);
  unsigned char *data = CGBitmapContextGetData (cgctx);
  // Releases the context created by the above function 
  return data;
// Set the background to the original image , That is, the image used for color selection 
- (void)setSourceImage:(NSString *)sourceImage ImageWidth:(int)_width ImageHeight:(int)_height {
  // Generates a background image of the specified size 
  UIImage *im = [UIImage imageNamed:sourceImage];
  UIImage *newImage;
  UIImageView *view = [[UIImageView alloc] initWithImage:im];
  view.frame = CGRectMake(0, 0, _width, _height);
  UIGraphicsBeginImageContext(CGSizeMake(_width, _height)); //size  for CGSize Type, that is, the size of the image you need 
  [im drawInRect:CGRectMake(0, 0, _width, _height)]; //newImageRect Specifies the image drawing area 
  newImage = UIGraphicsGetImageFromCurrentImageContext();
  width = newImage.size.width;
  height = newImage.size.height;
  // The background picture will be resolved into pixels for color selection 
  imgPixel = RequestImagePixelData(newImage);
// Calculation of the color 
-(UIColor*)calColor:(CGPoint)aPoint {
  int i = 4 * width * round(aPoint.y+imageView.frame.size.height/2) + 4 * round(aPoint.x+imageView.frame.size.width/2);
  int _r = (unsigned char)imgPixel[i];
  int _g = (unsigned char)imgPixel[i+1];
  int _b = (unsigned char)imgPixel[i+2];
  NSLog(@"Red : %f  Green: %f  Blue: %f",_r/255.0,_g/255.0,_b/255.0);
  return [UIColor colorWithRed:_r/255.0f green:_g/255.0f blue:_b/255.0f alpha:1.0];
- (void)changColor:(UIColor *)color{
  int width_;
  if (![Util isIpad]) {
    width_ = 30;
  } else {
    width_ = 70;
  UIGraphicsBeginImageContext(CGSizeMake(width_, width_));
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextMoveToPoint(ctx, 20, 20);
  CGContextSetFillColorWithColor(ctx, color.CGColor);
  if (![Util isIpad]) {
    CGContextAddArc(ctx, width_/2, width_/2, 14.5, 0, 6.3, 0);
  } else {
    CGContextAddArc(ctx, width_/2+0.5, width_/2, 31.3, 0, 6.3, 0);
  self->pickedColorImageView.image = UIGraphicsGetImageFromCurrentImageContext();

The above is the entire content of this article, I hope to help you with your study.

Related articles: