@@ -18,14 +18,10 @@ +(UIColor *)colorAtPoint:(CGPoint)point{
1818
1919 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext ();
2020
21-
2221 if (!CGRectContainsPoint (CGRectMake (0 .0f , 0 .0f , viewImage.size .width , viewImage.size .height ), point)) {
23-
2422 return nil ;
25-
2623 }
2724
28-
2925 NSInteger pointX = trunc (point.x );
3026
3127 NSInteger pointY = trunc (point.y );
@@ -46,19 +42,7 @@ +(UIColor *)colorAtPoint:(CGPoint)point{
4642
4743 unsigned char pixelData[4 ] = { 0 , 0 , 0 , 0 };
4844
49- CGContextRef context = CGBitmapContextCreate (pixelData,
50-
51- 1 ,
52-
53- 1 ,
54-
55- bitsPerComponent,
56-
57- bytesPerRow,
58-
59- colorSpace,
60-
61- kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
45+ CGContextRef context = CGBitmapContextCreate (pixelData, 1 , 1 , bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
6246
6347 CGColorSpaceRelease (colorSpace);
6448
@@ -86,120 +70,4 @@ +(UIColor *)colorAtPoint:(CGPoint)point{
8670 return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
8771}
8872
89-
90- +(UIImage *)fullScreenshots {
91-
92- UIWindow *screenWindow = [[UIApplication sharedApplication ] keyWindow ];
93- UIGraphicsBeginImageContext (screenWindow.frame .size );// 全屏截图,包括window
94-
95- [screenWindow.layer renderInContext: UIGraphicsGetCurrentContext ()];
96-
97- UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext ();
98-
99-
100- return viewImage;
101-
102- }
103-
104- +(UIColor*) getPixelColorAtLocation : (CGPoint)point withImage : (UIImage*) image {
105- UIColor* color = nil ;
106- CGImageRef inImage = image.CGImage ;
107- // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
108- CGContextRef cgctx = [self createARGBBitmapContextFromImage: inImage];
109- if (cgctx == NULL ) { return nil ; }
110-
111- size_t w = CGImageGetWidth (inImage);
112- size_t h = CGImageGetHeight (inImage);
113- CGRect rect = {{0 ,0 },{w,h}};
114-
115- // Draw the image to the bitmap context. Once we draw, the memory
116- // allocated for the context for rendering will then contain the
117- // raw image data in the specified color space.
118- CGContextDrawImage (cgctx, rect, inImage);
119-
120- // Now we can get a pointer to the image data associated with the bitmap
121- // context.
122- unsigned char * data = CGBitmapContextGetData (cgctx);
123- if (data != NULL ) {
124- // offset locates the pixel in the data from x,y.
125- // 4 for 4 bytes of data per pixel, w is width of one row of data.
126- @try {
127- int offset = 4 *((w*round (point.y ))+round (point.x ));
128- NSLog (@" offset: %d " , offset);
129- int alpha = data[offset];
130- int red = data[offset+1 ];
131- int green = data[offset+2 ];
132- int blue = data[offset+3 ];
133- NSLog (@" offset: %i colors: RGB A %i %i %i %i " ,offset,red,green,blue,alpha);
134- color = [UIColor colorWithRed: (red/255 .0f ) green: (green/255 .0f ) blue: (blue/255 .0f ) alpha: (alpha/255 .0f )];
135- }
136- @catch (NSException * e) {
137- NSLog (@" %@ " ,[e reason ]);
138- }
139- @finally {
140- }
141-
142- }
143-
144- return color;
145- }
146- + (CGContextRef) createARGBBitmapContextFromImage : (CGImageRef) inImage {
147-
148- CGContextRef context = NULL ;
149- CGColorSpaceRef colorSpace;
150- void * bitmapData;
151- int bitmapByteCount;
152- int bitmapBytesPerRow;
153-
154- // Get image width, height. We'll use the entire image.
155- size_t pixelsWide = CGImageGetWidth (inImage);
156- size_t pixelsHigh = CGImageGetHeight (inImage);
157-
158- // Declare the number of bytes per row. Each pixel in the bitmap in this
159- // example is represented by 4 bytes; 8 bits each of red, green, blue, and
160- // alpha.
161- bitmapBytesPerRow = (pixelsWide * 4 );
162- bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
163-
164- // Use the generic RGB color space.
165- colorSpace = CGColorSpaceCreateDeviceRGB ();
166-
167- if (colorSpace == NULL )
168- {
169- fprintf (stderr, " Error allocating color spacen" );
170- return NULL ;
171- }
172-
173- // Allocate memory for image data. This is the destination in memory
174- // where any drawing to the bitmap context will be rendered.
175- bitmapData = malloc ( bitmapByteCount );
176- if (bitmapData == NULL )
177- {
178- fprintf (stderr, " Memory not allocated!" );
179- CGColorSpaceRelease ( colorSpace );
180- return NULL ;
181- }
182-
183- // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
184- // per component. Regardless of what the source image format is
185- // (CMYK, Grayscale, and so on) it will be converted over to the format
186- // specified here by CGBitmapContextCreate.
187- context = CGBitmapContextCreate (bitmapData,
188- pixelsWide,
189- pixelsHigh,
190- 8 , // bits per component
191- bitmapBytesPerRow,
192- colorSpace,
193- kCGImageAlphaPremultipliedFirst );
194- if (context == NULL )
195- {
196- free (bitmapData);
197- fprintf (stderr, " Context not created!" );
198- }
199- // Make sure and release colorspace before returning
200- CGColorSpaceRelease ( colorSpace );
201-
202- return context;
203- }
204-
20573@end
0 commit comments