【问题标题】:get the uicolor of the tapped button获取点击按钮的 uicolor
【发布时间】:2011-02-14 10:49:13
【问题描述】:


我有一个屏幕,里面有很多不同颜色的按钮。我想要的是,当我按下某个按钮时,我会在该图像上应用 uiimage,然后我可以获得该点击按钮的 UIcolor 信息或 rgb 值。我知道我可以使用一些常量来实现该目的,但我想以动态和程序化的方式来实现。

更多的是整个按钮的图像是统一的。

【问题讨论】:

    标签: iphone button sdk uicolor


    【解决方案1】:

    @meronix

    感谢您的努力...
    我发现了一个类似的东西,我已经使用它并且它的工作......
    我想要 RGB 值来使颜色正常工作:)

    
    - (UIColor*)getRGBAsFromImage: (UIImage*)image atX: (int)xx andY: (int)yy
    {
    
    
    // First get the image into your data buffer
        CGImageRef imageRef = [image CGImage];
        NSUInteger width = CGImageGetWidth(imageRef);
        NSUInteger height = CGImageGetHeight(imageRef);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char *rawData = malloc(height * width * 4);
        NSUInteger bytesPerPixel = 4;
        NSUInteger bytesPerRow = bytesPerPixel * width;
        NSUInteger bitsPerComponent = 8;
        CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                     bitsPerComponent, bytesPerRow, colorSpace,
                                                     kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);
    
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        CGContextRelease(context);
    
        // Now your rawData contains the image data in the RGBA8888 pixel format.
        int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
            CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
            CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
            CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
            CGFloat alpha = 1.0;//(rawData[byteIndex + 3] * 1.0) / 255.0;
            byteIndex += 4;
    
            UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    
    
    
        free(rawData);
    
        return acolor;
    
    }
    
    

    【讨论】:

      【解决方案2】:

      使用 IBAction 的 (id)sender 参数。这是对收到点击的对象的引用,您可以使用该引用来获取所需的任何信息。

      【讨论】:

      • 是的,我知道我可以通过发件人获取动作执行者的信息,我也可以从该发件人那里获取图像,但现在我有了图像,我想要该图像的 rgb 或颜色值...我该怎么做,,,
      【解决方案3】:

      Abizern 是仪式,我会给你提供示例代码,

      - (void)onTouchUpAction:(id)sender {
      
          //get the address of the button to your variable.
          UIButton *button = (UIButton*)sender;
      
          //change the background color.
          [button setBackgroundColor:[UIColor yourColor]];
      
          //remove address from the unused pointer
          button = nil;
      
      }
      

      【讨论】:

      • 嗨,jayahari,我认为我的问题不太清楚,所以我编辑了它,希望它会更清楚。我知道当我按下按钮时,我可以通过(id)sender 获得它的每一个功能。我想要做的是获取应用于该按钮的 uiimage 的 uicolor 或其 rgb 值。
      • @yunas:你要找的颜色是按钮的背景色吗?还是图片中的颜色?
      【解决方案4】:

      *>我说的是颜色

      图像和图像具有均匀性 颜色... – yunas 1 小时前*

      那么您可能会发现这些方法很有用:

      - (UIColor*) getPixelColorAtLocation:(CGPoint)point {
          UIColor* color = nil;
          CGImageRef inImage;
          inImage = self.image.CGImage;
      
          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}}; 
      
          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]; 
              color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
          }
      
          // When finished, release the context
          CGContextRelease(cgctx); 
          // Free image data memory for the context
          if (data) { free(data); }
          return color;
      }
      
      
      
      - (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {
      
          CGContextRef    context = NULL;
          CGColorSpaceRef colorSpace;
          void *          bitmapData;
          int             bitmapByteCount;
          int             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;
      }
      

      你可能需要换行:

      inImage = self.image.CGImage;
      

      使用按钮中使用的图像:

      inImage = yourButton.imageView.image.CGImage;

      然后打电话

      getPixelColorAtLocation:aPoint // the centre of button...?
      

      要返回颜色...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2020-11-29
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        相关资源
        最近更新 更多