Eventually:
UIImage classification, Point2color:
uiimage+point2color.h// 26_popoverctrl//// Created by Beyond on 14-8-31.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface UIImage (point2color)//pass in a point coordinate, Returns the Color object of the point on the picture-(Uicolor *) Colorfrompoint: (cgpoint) points; @end
uiimage+point2color.m//26_popoverctrl////Created by Beyond on 14-8-31.//Copyright (c) 2014 Com.beyond. All rights reserved.//class, pass in a point, get the color of that dot #import "Uiimage+point2color.h" @implementation UIImage (Point2color)//Incoming A point coordinate that returns the color object of the point on the picture//writes the picture to memory and then takes the color from "point"-(Uicolor *) Colorfrompoint: (cgpoint) Point{uicolor *color = nil; Get a reference to the color picture cgimageref inimage = self. Cgimage; Call a custom method: Create and return the corresponding context cgcontextref contexref = [self argbbitmapcontextfromimage:inimage] from the reference to the image inside the _imgview; If the context for creating the picture fails if (Contexref = = NULL) {NSLog (@ "Take color picture--Create corresponding context failed ~"); return nil; }//ready to write "Take color picture" to the context just created size_t w = cgimagegetwidth (inimage); size_t h = cgimagegetheight (inimage); CGRect rect = {{0,0},{w,h}}; Debug output rect:--{{0, 0}, {225, 250}}//write bitmap (render) already created context workspace Cgcontextdrawimage (contexref, rect, inimage);//plot Context memory data The first address of the block, with pointers to remember, as the base address unsigned char* Datapoint = Cgbitmapcontextgetdata (contexref); NSLog (@ "----first address, pointer%p", datapoint); ----first address, pointer 0x8b3f000if (datapoint! = NULL) {//offset: Locates a specific pixel in the bitmap's memory space based on the xy of the touch point//4 means that each pixel is 4 bytes//w is the total of all points per line /According to the row, the column, the offset address in the memory block, and then multiplied by 4, because each point in memory accounted for four bytes int offset = w*round (POINT.Y) +round (point.x)); Alpha for memory base address + offset address int alpha = Datapoint[offset]; Red for memory base address + offset addresses +1 other similar int red = Datapoint[offset+1];int Green = Datapoint[offset+2];int blue = datapoint[offset+3]; NSLog (@ "offset address:%i Colors:rgba%i%i%i%i", Offset,red,green,blue,alpha); offset:150908 Colors:rgb A 255 0 254 255//Generate Color object based on rgba colour = [Uicolor colorwithred: (red/255.0f) Green: (gr een/255.0f) Blue: (blue/255.0f) Alpha: (alpha/255.0f)];} When the operation is complete, release the context object Cgcontextrelease (CONTEXREF);//Release the image data loaded into memory from memory if (datapoint) {free (datapoint);} Returns the color of the point corresponding to the image on the picture return color;} Custom Method 2: Create and return the corresponding context through the reference to the image inside the _imgview, which is to create a argbbitmapcontext-(CGCONTEXTREF) based on Cgimageref Argbbitmapcontextfromimage: (CGIMAGEREF) inimage{//contexts to be created cgcontextref context = NULL; ColorColor space Cgcolorspaceref colorspace; Bitmap data at the first address of the memory space void * BITMAPDATA; The number of bytes per line int bitmapbytesperrow; The total number of bytes of the picture int bitmapbytecount;//Gets the width and height of the picture, will use the entire picture, create the context size_t Pixelswide = Cgimagegetwidth (inimage); s ize_t Pixelshigh = Cgimagegetheight (inimage);//How many bytes each line occupies. 4 bytes per pixel in this color-fetching picture; Red, green, and blue transparency accounts for one byte (8-bit range 0~255)//bytes per row, because each pixel occupies 4 bytes (containing Rgba) (where one R is a byte, 8 bits, and a value of 2 is 8 0~255) Bitmapbytesperrow = (Pixelswide * 4); The total number of bytes in the picture Bitmapbytecount = (Bitmapbytesperrow * pixelshigh);//using the specified color space (RGB) colorspace = Cgcolorspacecreatedevice RGB (); if (colorspace = = NULL) {fprintf (stderr, "Error creating and allocating color space \ n"); return NULL;} Allocate all memory space for the color picture data//all operations that draw the picture context will be rendered to this memory space BitmapData = malloc (Bitmapbytecount); if (bitmapData = = NULL) {Fprin TF (stderr, "Memory space allocation failed ~"); Cgcolorspacerelease (colorspace); return NULL;} Creates a bitmap context. With Pre-multiplied ARGB, each member of the ARGB occupies 8 bits, that is, one byte, and one pixel total of 4 bytes//Regardless of the format of the original color picture (CMYK or grayscale), the CgbitmapcontextcreAte method, converted to the specified ARGB format context = Cgbitmapcontextcreate (BitmapData, Pixelswide, Pixelshigh, 8,//bits per component bitmap Bytesperrow, ColorSpace, (Cgbitmapinfo) kcgimagealphapremultipliedfirst); if (context = = NULL) {free (bitmapData); fprintf (stderr, "Bitmap context creation failed ~");} You must remember to release the color space before returning to the context cgcolorspacerelease (colorspace); return context;} @end
ColorPicker Controller and its agent
colorpickercontroller.h// 26_popoverctrl//// Created by Beyond on 14-8-31.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @protocol colorpickerdelegate; @interface Colorpickercontroller: uiviewcontroller//members: Agent, to the time of notification of the agent, the choice of color is what ~ @property (weak, nonatomic) id<colorpickerdelegate> delegate; @end
colorpickercontroller.m//26_popoverctrl////Created by Beyond on 14-8-31.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "ColorPickerController.h"//Category #import "Uiimage+point2color.h"//color selection complete, notify Agent #import " ColorPickerDelegate.h "@interface Colorpickercontroller () @property (weak, nonatomic) Iboutlet Uiimageview *imageview; @end @implementation Colorpickercontroller#pragma Mark-life cycle Method-(void) viewdidload{[Super Viewdidload]; Important ~ ~ ~ Specifies the size (as in the picture) that the current controller displays in PopOver self.preferredcontentsize = self.imageView.image.size;} At the end of the touch, get the coordinates of the click, call the UIImage classification method, get the color of the point on the picture-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{// Someone needs the color of that point before taking the color if ([Self.delegate respondstoselector: @selector (colorpickercontroller:didselectedcolor:)]) {// Get touch point Uitouch *touch = [touches anyobject]; Cgpoint point = [Touch LocationInView:touch.view]; Get color Uicolor *color = [Self.imageView.image colorfrompoint:point]; Tell the agent that the point corresponds to the color [self.delegate colorpickercontroller:self didselectedcolor:color]; }} @end
colorpickerdelegate.h// 26_popoverctrl//// Created by Beyond on 14-8-31.// Copyright (c) 2014 Com.beyond. All rights reserved.// color Select the agent of the Controller, when it decodes the color of the user click, notify the agent #import <Foundation/Foundation.h> @class Colorpickercontroller, @protocol colorpickerdelegate <NSObject> @optional-(void) Colorpickercontroller: ( Colorpickercontroller *) Ctrl Didselectedcolor: (Uicolor *) color; @end
Master Controller
beyondviewcontroller.m//26_popoverctrl////Created by Beyond on 14-8-31.//Copyright (c) 2014 Com.beyond. All rights reserved.//00000000//99999999#import "BeyondViewController.h"//click on the left item, eject Nana controller #import " NanaViewController.h "//Click the middle button, pop-up color Select controller #import" ColorPickerController.h "//Proxy method #import" ColorPickerDelegate.h "@ Interface Beyondviewcontroller () <colorpickerdelegate>-(ibaction) Menuclick: (Uibarbuttonitem *) sender;-( ibaction) Colorbuttonclick: (UIButton *) sender;//Uipopovercontroller cannot be a local variable, must be a member variable @property (nonatomic, Strong) Uipopovercontroller *menupopover;//Uipopovercontroller cannot be a local variable, it must be a member variable @property (nonatomic, Strong) Uipopovercontroller *colorpopover; @end @implementation beyondviewcontroller#pragma mark-Lazy Load Getter Method-( Uipopovercontroller *) menupopover{if (_menupopover = = nil) {//1. Create content controller Nanaviewcontroller *nana = [[N Anaviewcontroller alloc] init]; Uinavigationcontroller *nav = [[Uinavigationcontroller alloc] InitwithrootViewcontroller:nana]; 2. Create PopOver self.menupopover = [[Uipopovercontroller alloc] initwithcontentviewcontroller:nav]; }//return popover return _menupopover;} -(Uipopovercontroller *) colorpopover{if (_colorpopover = = nil) {//1. Creating a content Controller Colorpickercontroller *CP VC = [[Colorpickercontroller alloc] init]; Cpvc.delegate = self; 2. Create PopOver self.colorpopover = [[Uipopovercontroller alloc] INITWITHCONTENTVIEWCONTROLLER:CPVC]; Important ~ ~ ~ Click the shadow outside the PopOver so that the click event can penetrate ...//self.colorPopover.passthroughViews = @[self.colorbutton]; } return _colorpopover;} #pragma mark-Proxy method for color selection controller-(void) Colorpickercontroller: (Colorpickercontroller *) CPVC didselectedcolor: (Uicolor *) color{self.view.backgroundColor = color;} #pragma mark-Listen button click/** * Click menu, in the specified position, pop popover */-(ibaction) Menuclick: (Uibarbuttonitem *) sender{//show to which location [self . Menupopover Presentpopoverfrombarbuttonitem:sender PermittedarrowdirectIons:uipopoverarrowdirectionany Animated:yes];} /** * Clicked on the Color button, in the specified position, pops up PopOver */-(ibaction) Colorbuttonclick: (UIButton *) sender{[Self.colorpopover Presentpopoverfro MRect:sender.bounds Inview:sender permittedarrowdirections:uipopoverarrowdirectionany Animated:yes];} @end
Ios_26popovercontroller Simple to use