zoukankan      html  css  js  c++  java
  • Touch event in certain color rect

     UIImage+ColorAtPixel category

    #import <UIKit/UIKit.h>

    /*

     A category on UIImage that enables you to query the color value of arbitrary 

     pixels of the image.

     */

    @interface UIImage (ColorAtPixel)

    - (UIColor *)colorAtPixel:(CGPoint)point;

    @end

    #import <CoreGraphics/CoreGraphics.h>

    #import "UIImage+ColorAtPixel.h"

    @implementation UIImage (ColorAtPixel)

    /*

     Returns the color of the image pixel at point. Returns nil if point lies outside the image bounds.

     If the point coordinates contain decimal parts, they will be truncated.

     To get at the pixel data, this method must draw the image into a bitmap context.

     For minimal memory usage and optimum performance, only the specific requested

     pixel is drawn.

     If you need to query pixel colors for the same image repeatedly (e.g., in a loop),

     this approach is probably less efficient than drawing the entire image into memory

     once and caching it.

     */

    - (UIColor *)colorAtPixel:(CGPoint)point {

        // Cancel if point is outside image coordinates

        if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {

            return nil;

        }

        // Create a 1x1 pixel byte array and bitmap context to draw the pixel into.

        // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage

        NSInteger pointX = trunc(point.x);

        NSInteger pointY = trunc(point.y);

        CGImageRef cgImage = self.CGImage;

        NSUInteger width = CGImageGetWidth(cgImage);

        NSUInteger height = CGImageGetHeight(cgImage);

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        int bytesPerPixel = 4;

        int bytesPerRow = bytesPerPixel * 1;

        NSUInteger bitsPerComponent = 8;

        unsigned char pixelData[4] = { 0, 0, 0, 0 };

        CGContextRef context = CGBitmapContextCreate(pixelData, 

                                                     1,

                                                     1,

                                                     bitsPerComponent, 

                                                     bytesPerRow, 

                                                     colorSpace,

                                                     kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

        CGColorSpaceRelease(colorSpace);

        CGContextSetBlendMode(context, kCGBlendModeCopy);

        // Draw the pixel we are interested in onto the bitmap context

        CGContextTranslateCTM(context, -pointX, -pointY);

        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);

        CGContextRelease(context);

        // Convert color values [0..255] to floats [0.0..1.0]

        CGFloat red   = (CGFloat)pixelData[0] / 255.0f;

        CGFloat green = (CGFloat)pixelData[1] / 255.0f;

        CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;

        CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;

        return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

    }

    @end

    Custom button

    #define kAlphaVisibleThreshold (0.1f);

    @interface OBShapedButton : UIButton {

        // Our class interface is empty. OBShapedButton only overwrites one method of UIView.

        // It has no attributes of its own.

    }

    @end

    #import "OBShapedButton.h"

    #import "UIImage+ColorAtPixel.h"

    @implementation OBShapedButton

    // UIView uses this method in hitTest:withEvent: to determine which subview should receive a touch event.

    // If pointInside:withEvent: returns YES, then the subview’s hierarchy is traversed; otherwise, its branch

    // of the view hierarchy is ignored.

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

        // Return NO if even super returns NO (i.e., if point lies outside our bounds)

        BOOL superResult = [super pointInside:point withEvent:event];

        if (!superResult) {

            return superResult;

        }

        // We can't test the image's alpha channel if the button has no image. Fall back to super.

        UIImage *buttonImage = [self backgroundImageForState:UIControlStateNormal];

        if (buttonImage == nil) {

            return YES;

        }

        CGColorRef pixelColor = [[buttonImage1 colorAtPixel:point] CGColor];//[[buttonImage1 colorAtPixel:point] CGColor];

        CGFloat alpha = CGColorGetAlpha(pixelColor);

        return alpha >= kAlphaVisibleThreshold;

    }

    @end

  • 相关阅读:
    题解[LuoguP7419 「PMOI-2」参天大树]
    UVA11582 巨大的斐波那契数! Colossal Fibonacci Numbers!
    数学专题
    [计蒜客]dp
    [蓝桥杯每日一题]1.3 & 1.4
    【acm】2020icpc南京补题
    [acm]乐师师范学院校赛题解-2020
    西南交通大学峨眉校区第二届"INT"杯程序设计竞赛——决赛
    指针与结构体
    [acm] 动态规划——最长上升子序列
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2458489.html
Copyright © 2011-2022 走看看