This article explains the code of iOS taking colors from the background image, and shares it with you for your reference. The specific content is as follows
Implementation code:
void *bitmapData; //Pointer to memory space, the size of this memory space is equal to the number of bytes occupied by the image using RGB channel. static CGContextRef CreateRGBABitmapContext (CGImageRef inImage) { CGContextRef context = NULL; CGColorSpaceRef colorSpace; int bitmapByteCount; int bitmapBytesPerRow; size_t pixelsWide = CGImageGetWidth(inImage); //Get the number of horizontal pixel points size_t pixelsHigh = CGImageGetHeight(inImage); bitmapBytesPerRow = (pixelsWide * 4); //The number of bytes occupied by pixels in each row, and the four ARGB channels of each pixel occupy 8 bits (0-255) space. bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); //Calculate the number of bytes occupied by the entire picture colorSpace = CGColorSpaceCreateDeviceRGB();//Create RGB channels that depend on the device //Allocate enough memory space to accommodate the number of image bytes bitmapData = malloc( bitmapByteCount ); // Create a graphical context for CoreGraphic, which describes some drawing parameters of the image that needs to be drawn by bitmaData pointing to the memory space of the image that needs to be drawn context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); //Pointers created in the Core Foundation through the method names containing Create and Alloc need to be released using the CFRelease() function CGColorSpaceRelease( colorSpace ); return context; } // Return a pointer, which points to an array, and every four elements in the array are the RGBA values (0-255) of a pixel point on the image. Use unsigned char because its perfect value range is 0-255static unsigned char *RequestImagePixelData(UIImage *inImage) { CGImageRef img = [inImage CGImage]; CGSize size = [inImage size]; //Create context using the above function CGContextRef cgctx = CreateRGBABitmapContext(img); CGRect rect = {{0,0},{, }}; //Draw the target image to the specified context, which is actually a bitmapData within the context. CGContextDrawImage(cgctx, rect, img); unsigned char *data = CGBitmapContextGetData (cgctx); //Release the context created by the above function CGContextRelease(cgctx); return data; } //Set the original background picture, that is, the picture used to obtain the color- (void)setSourceImage:(NSString *)sourceImage ImageWidth:(int)_width ImageHeight:(int)_height { // Generate a background image of the specified size UIImage *im = [UIImage imageNamed:sourceImage]; UIImage *newImage; UIImageView *view = [[UIImageView alloc] initWithImage:im]; = CGRectMake(0, 0, _width, _height); UIGraphicsBeginImageContext(CGSizeMake(_width, _height)); //size is CGSize type, that is, the image size you need [im drawInRect:CGRectMake(0, 0, _width, _height)]; //newImageRect specifies the image drawing area newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); width = ; height = ; //Parse the background image as pixels for color use imgPixel = RequestImagePixelData(newImage); } //Calculate the color-(UIColor*)calColor:(CGPoint)aPoint { int i = 4 * width * round(+/2) + 4 * round(+/2); int _r = (unsigned char)imgPixel[i]; int _g = (unsigned char)imgPixel[i+1]; int _b = (unsigned char)imgPixel[i+2]; NSLog(@"(%f,%f)",,); 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, ); 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); } CGContextFillPath(ctx); self-> = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }
The above is all about this article, I hope it will be helpful to everyone's learning.