【问题标题】:Detect black out line on an image ios检测图像ios上的黑线
【发布时间】:2013-10-31 09:31:39
【问题描述】:

我有一个 .PNG 文件。该图像具有白色背景,只有黑色线条。

我想在用户触摸图像时检测到黑线。在这张图片中,我如何通过触摸检测出花朵的线条

例如

这怎么可能?

请帮帮我……

【问题讨论】:

  • 我的问题有什么问题?为什么要投反对票?

标签: ios ipad cocoa-touch core-graphics


【解决方案1】:

好的解决方案只有这个类会帮助UIView+ColorOfPoint

【讨论】:

    【解决方案2】:

    创建UIView类的类别。添加以下函数。

    @implementation UIView (ColorOfPoint)
    
    - (UIColor *) colorOfPoint:(CGPoint)point
    {
          unsigned char pixel[4] = {0};
          CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
          CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapFloatComponents);
          CGContextTranslateCTM(context, -point.x, -point.y);
          [self.layer renderInContext:context];
          CGContextRelease(context);
          CGColorSpaceRelease(colorSpace);
          UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
    
          return color;
    }
    @end
    

    通过以下调用使用它:

      UIColor *col = [YOUR_IMAGE_VIEW colorOfPoint:CGPOINT_WHERE_USER_TOUCHED];
    

    【讨论】:

    • 没有分类也可以吗?
    • 是的.. 将其作为您的正常功能并将您的 UIImageView 和 CGPoint 作为参数传递。
    • 显示错误属性 'layer' not found on object of type 'classname *'
    • 使用 imageview.layer 而不是 self.layer,因为它现在从类别移动到普通类函数。
    • 只有alpha值发生变化,有时显示0有时显示1,没有得到完美的RGB和alpha
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 2023-03-08
    • 2017-12-01
    • 2020-02-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多