实现不规则形状的按钮
用到了四个文件:
UIImage+ColorAtPixel.h UIImage+ColorAtPixel.m OBShapedButton.h OBShapedButton.m
作者:Ole Begemann
1 /* 2 Copyright (c) 2009 Ole Begemann 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21 */ 22 23 /* 24 UIImage+ColorAtPixel.h 25 26 Created by Ole Begemann 27 October, 2009 28 */ 29 30 #import <UIKit/UIKit.h> 31 32 /* 33 A category on UIImage that enables you to query the color value of arbitrary 34 pixels of the image. 35 */ 36 @interface UIImage (ColorAtPixel) 37 38 - (UIColor *)colorAtPixel:(CGPoint)point; 39 40 @end
1 /* 2 Copyright (c) 2009 Ole Begemann 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21 */ 22 23 /* 24 UIImage+ColorAtPixel.m 25 26 Created by Ole Begemann 27 October, 2009 28 */ 29 30 #import <CoreGraphics/CoreGraphics.h> 31 32 #import "UIImage+ColorAtPixel.h" 33 34 35 @implementation UIImage (ColorAtPixel) 36 37 /* 38 Returns the color of the image pixel at point. Returns nil if point lies outside the image bounds. 39 If the point coordinates contain decimal parts, they will be truncated. 40 41 To get at the pixel data, this method must draw the image into a bitmap context. 42 For minimal memory usage and optimum performance, only the specific requested 43 pixel is drawn. 44 If you need to query pixel colors for the same image repeatedly (e.g., in a loop), 45 this approach is probably less efficient than drawing the entire image into memory 46 once and caching it. 47 */ 48 - (UIColor *)colorAtPixel:(CGPoint)point { 49 // Cancel if point is outside image coordinates 50 if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) { 51 return nil; 52 } 53 54 55 // Create a 1x1 pixel byte array and bitmap context to draw the pixel into. 56 // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage 57 NSInteger pointX = trunc(point.x); 58 NSInteger pointY = trunc(point.y); 59 CGImageRef cgImage = self.CGImage; 60 NSUInteger width = self.size.width; 61 NSUInteger height = self.size.height; 62 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 63 int bytesPerPixel = 4; 64 int bytesPerRow = bytesPerPixel * 1; 65 NSUInteger bitsPerComponent = 8; 66 unsigned char pixelData[4] = { 0, 0, 0, 0 }; 67 CGContextRef context = CGBitmapContextCreate(pixelData, 68 1, 69 1, 70 bitsPerComponent, 71 bytesPerRow, 72 colorSpace, 73 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 74 CGColorSpaceRelease(colorSpace); 75 CGContextSetBlendMode(context, kCGBlendModeCopy); 76 77 // Draw the pixel we are interested in onto the bitmap context 78 CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height); 79 CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage); 80 CGContextRelease(context); 81 82 // Convert color values [0..255] to floats [0.0..1.0] 83 CGFloat red = (CGFloat)pixelData[0] / 255.0f; 84 CGFloat green = (CGFloat)pixelData[1] / 255.0f; 85 CGFloat blue = (CGFloat)pixelData[2] / 255.0f; 86 CGFloat alpha = (CGFloat)pixelData[3] / 255.0f; 87 return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 88 } 89 90 @end