//
// UIImageView+MyImageView.m
// ZHBWorker
//
// Created by JackRen on 16/6/23.
// Copyright © 2016年 atense. All rights reserved.
//
#import "UIImageView+MyImageView.h"
@implementation UIImageView (MyImageView)
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// unsigned char pixel[1] = {0};
// CGContextRef context = CGBitmapContextCreate(pixel,
// 1, 1, 8, 1, NULL,
// kCGImageAlphaOnly);
// UIGraphicsPushContext(context);
// [self.image drawAtPoint:CGPointMake(-point.x, -point.y)];
// UIGraphicsPopContext();
// CGContextRelease(context);
// CGFloat alpha = pixel[0]/255.0f;
// NSLog(@"%f==%f==%f==",alpha,-point.x,-point.y);
// BOOL transparent = alpha < 0.01f;
// CGImageRef image = self.image.CGImage;
// NSUInteger width = CGImageGetWidth(image);
// NSUInteger height = CGImageGetHeight(image);
// CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
rawData = malloc(height * width * 4);
// NSUInteger bytesPerPixel = 4;
// NSUInteger bytesPerRow = bytesPerPixel * width;
//
// NSUInteger bitsPerComponent = 8;
// CGContextRef context = CGBitmapContextCreate(
// pixel, width, height, bitsPerComponent, bytesPerRow, colorSpace,
// kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
// );
CGColorSpaceRelease(colorSpace);
// UIGraphicsPushContext(context);
// [self.image drawAtPoint:CGPointMake(-point.x, -point.y)];
// UIGraphicsPopContext();
// CGContextRelease(context);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGContextRelease(context);
int byteIndex = (bytesPerRow * uiViewPoint.y) + uiViewPoint.x * bytesPerPixel;
unsigned char alpha = rawData[byteIndex + 3];
// CGFloat alpha = pixel[0]/255.0f;
// NSLog(@"%f==%f==%f==",alpha,-point.x,-point.y);
// BOOL transparent = alpha < 0.01f;
return [self getPixelColorAtLocation:point];
}
- (BOOL) getPixelColorAtLocation:(CGPoint)point {
// UIColor* color = nil;
int alpha = 0;
CGImageRef inImage = self.image.CGImage;
NSLog(@"%@", inImage);
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { return nil; }
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage);
// Now we can get a pointer to the image data associated with the bitmap
// context.
unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL) {
//offset locates the pixel in the data from x,y.
//4 for 4 bytes of data per pixel, w is width of one row of data.
@try {
int offset = 4*((w*round(point.y))+round(point.x));
NSLog(@"offset: %d", offset);
alpha = data[offset];
int red = data[offset+1];
int green = data[offset+2];
int blue = data[offset+3];
NSLog(@"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha);
// color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
}
@catch (NSException * e) {
NSLog(@"%@",[e reason]);
}
@finally {
}
}
// When finished, release the context
CGContextRelease(cgctx);
// Free image data memory for the context
if (data) { free(data); }
NSLog(@"%d",alpha);
return alpha == 255;
}
//
//创建取点图片工作域:
- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
long bitmapByteCount;
long bitmapBytesPerRow;
// Get image width, height. We'll use the entire image.
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease( colorSpace );
return NULL;
}
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
free (bitmapData);
fprintf (stderr, "Context not created!");
}
// Make sure and release colorspace before returning
CGColorSpaceRelease( colorSpace );
return context;
}
@end
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint centerpoint = CGPointMake(170.0f, 170.0f);
[self getIndexForCenter:centerpoint forCurPoint:point forMaxRadius:168*HomePageDefineWidth forMinRadius:50*HomePageDefineWidth];
if ([self getIndexForCenter:centerpoint forCurPoint:point forMaxRadius:168*HomePageDefineWidth forMinRadius:50*HomePageDefineWidth] == 1) {
[self RetrogradeRotation];
}else if ([self getIndexForCenter:centerpoint forCurPoint:point forMaxRadius:168*HomePageDefineWidth forMinRadius:50*HomePageDefineWidth] == 2) {
[self PositiveRotation];
}else if ([self getIndexForCenter:centerpoint forCurPoint:point forMaxRadius:168*HomePageDefineWidth forMinRadius:50*HomePageDefineWidth] == 3) {
[self diapTheThreeSection];
}
// CGPoint hitRedPoint = [self convertPoint:point toView:self.animationRedImageView];
//
// CGPoint hitBluePoint = [self convertPoint:point toView:self.animationBlueImageView];
//
// CGPoint hitGreenPoint = [self convertPoint:point toView:self.animationGreenImageView];
//
//
//
// if ([self.animationBlueImageView pointInside:hitBluePoint withEvent:event]) {
// NSLog(@"蓝色");
// return self.animationBlueImageView;
// }
//
// if ([self.animationGreenImageView pointInside:hitGreenPoint withEvent:event]) {
// NSLog(@"绿色");
// return self.animationGreenImageView;
// }
//
// if ([self.animationRedImageView pointInside:hitRedPoint withEvent:event]) {
// NSLog(@"红色");
// return self.animationRedImageView;
// }
//
return nil;
}