UIColor使用

UIColor使用从图片中获得某点颜色属性-(UIColor*)getPixelColorAtLocationCGPoint)point{UIColor*color=nil;CGImageRefinImage=self.image.CGImage;//Createoffscreenbitmapcontexttodraw…

大家好,欢迎来到IT知识分享网。

从图片中获得某点颜色属性

– (UIColor *) getPixelColorAtLocationUIColor使用CGPoint)point {

        UIColor* color = nil;
        CGImageRef inImage = self.image.CGImage;
        // 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; /* error */
        }
        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.
                int offset = 4*((w*round(point.y))+round(point.x));
                int 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 colorWithRedUIColor使用red/255.0f) greenUIColor使用green/255.0f) blueUIColor使用blue/255.0f) alphaUIColor使用alpha/255.0f)];               
        }    
        // When finished, release the context
        CGContextRelease(cgctx);
        // Free image data memory for the context
        if (data) { free(data); }
        return color;
}

 

颜色属性

可以用一个UIColor对象来定义文字的色彩。UIColor这个类提供了许多不同的方法,可以很轻松地调出任何颜色。你可以用静态方法来创建 颜色,这样它们会在停止使用后被释放。可以用灰度值、色相或者RGB复合值等多种形式来创建颜色。要创建一个简单的RGB色彩,可以指定一组4个浮点值, 分别对应红、绿、蓝和alpha值(透明度),取值均在0.0~1.0之间。这些值表示了0%(0.0)~100%(1.0)的范围:

  1. UIColor *myWhiteTransparentColor = [ UIColor 
    colorWithWhite: 1.0 alpha: 0.50 ];  
  2.  
  3. UIColor *myColorHue = [ UIColor colorWithHue: 120.0 / 360.0  
  4.         saturation: 0.75  
  5.         brightness: 0.50  
  6.         alpha: 1.0  
  7. ];  
  8.  
  9. UIColor *myColorRGB = [ UIColor colorWithRed: 0.75  
  10.         green: 1.0  
  11.         blue: 0.75  
  12.         alpha: 1.0  
  13. ]; 

如果你打算重用许多不同的UIColor对象,你也可以创建它们的实例:

  1. UIColor *myWhiteTransparentColor = [ [ UIColor alloc ]  
  2.         initWithWhite: 1.0 alpha: 0.50  
  3. ];  
  4.  
  5. UIColor *myColorHue = [ [ UIColor alloc ]  
  6.         initWithHue: 120.0 / 360.0  
  7.         saturation: 0.75  
  8.         brightness: 0.50  
  9.         alpha: 1.0  
  10. ];  
  11.  
  12. UIColor *myColorRGB = [ [ UIColor alloc ] initWithRed: 0.75  
  13.         green: 1.0  
  14.         blue: 0.75  
  15.         alpha: 1.0  
  16. ]; 

UIColor类还支持许多静态方法,可以创建系统颜色,这些颜色都经过iPhone的校正,以达到尽可能准确的地步。这些方法如下所示,均来自UIColor.h:

  1. + (UIColor *)blackColor;        // 0.0 白色  
  2. + (UIColor *)darkGrayColor;     // 0.333 白色  
  3. + (UIColor *)lightGrayColor;    // 0.667 白色  
  4. + (UIColor *)whiteColor;        // 1.0 白色  
  5. + (UIColor *)grayColor;         // 0.5 白色  
  6. + (UIColor *)redColor;          // 1.0, 0.0, 0.0 RGB  
  7. + (UIColor *)greenColor;        // 0.0, 1.0, 0.0 RGB  
  8. + (UIColor *)blueColor;         // 0.0, 0.0, 1.0 RGB  
  9. + (UIColor *)cyanColor;         // 0.0, 1.0, 1.0 RGB  
  10. + (UIColor *)yellowColor;       // 1.0, 1.0, 0.0 RGB  
  11. + (UIColor *)magentaColor;      // 1.0, 0.0, 1.0 RGB  
  12. + (UIColor *)orangeColor;       // 1.0, 0.5, 0.0 RGB  
  13. + (UIColor *)purpleColor;       // 0.5, 0.0, 0.5 RGB  
  14. + (UIColor *)brownColor;        // 0.6, 0.4, 0.2 RGB  
  15. + (UIColor *)clearColor;        // 0.0 白色, 0.0 alpha 

创建好UIColor对象之后,就可以将其赋给文本视图的色彩属性了:

textView.textColor = myColorHue;

 

转载于:https://www.cnblogs.com/li-baibo/archive/2012/12/19/2824393.html

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/20868.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信