【问题标题】:IOS: use a .png to colour in a viewIOS:使用 .png 在视图中着色
【发布时间】:2012-03-21 09:47:38
【问题描述】:

我有这个代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:drawImage];

UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1.0); //black

CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

使用此代码,我用黑线在视图中着色,但我想用特定的 png 着色(例如,作为画笔);并具有特定的效果;我应该做些什么改变?

【问题讨论】:

    标签: ios xcode cgcontext brush touchesmoved


    【解决方案1】:

    我编写了一个从图像中的 CGPoint 获取颜色的方法: ImageOperations.h:

    + (UIColor *)getColorFromImage:(UIImage*)image atX:(int)x andY:(int)y;
    

    ImageOperations.m

    + (UIColor *)getColorFromImage:(UIImage*)image atX:(int)x andY:(int)y {
        CGImageRef imageRef = [image CGImage];
        NSUInteger width = CGImageGetWidth(imageRef);
        NSUInteger height = CGImageGetHeight(imageRef);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char *rawData = calloc(height * width * 4, sizeof(unsigned char));
        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);
    
        int byteIdx = (bytesPerRow * y) + x * bytesPerPixel;
    
        CGFloat red   = (rawData[byteIdx]     * 1.0) / 255.0;
        CGFloat green = (rawData[byteIdx + 1] * 1.0) / 255.0;
        CGFloat blue  = (rawData[byteIdx + 2] * 1.0) / 255.0;
        CGFloat alpha = (rawData[byteIdx + 3] * 1.0) / 255.0;
        byteIdx += 4;
    
        UIColor *acolor = [UIColor colorWithRed:red/255.f
                                          green:green/255.f
                                           blue:blue/255.f    
                                          alpha:alpha/255.f];      
        free(rawData);
    
        return acolor;
    }
    

    方法调用如下所示:

    UIColor *myColor= [ImageOperations getColorFromImage:myImage atX:cgPoint.x andY:cgPoint.y];
    
    self.myView.backgroundColor = myColor;
    

    希望这会有所帮助。

    【讨论】:

    • 不,你不明白我的问题;我想要一个视图中的颜色,就好像我使用画笔而不是一个简单的点一样!!!
    • 你的问题很模糊,没有理由生气。我的理解是,您正在尝试从 png(图像)中获取颜色并使用该颜色来设置 UIView 的背景颜色。如果这不正确,请编辑您的问题。
    • 我发布了一个例子,展示了如何用简单的线条着色,然后我想用 png 着色
    【解决方案2】:

    如果您的 png 位图只有单一颜色,那么您可以设置该 rgb 值。否则,如果您想像路径一样绘制 png 本身,请使用与使用点绘制线相同的技术..继续放置点的集合将给出u 线尝试通过放置 png 图像来实现这一点。 我的意思是你的单个 png 表现得像点。

    【讨论】:

    • 我希望我能帮助你了解一些样本..我以前从未尝试过,但乍一看这可能是我想到的解决方案..
    【解决方案3】:
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha);
    in your code  u set r g b -->0
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1.0); 
    
    that why its coming in black color, u need to set some value e.g
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 20, 50, 10, 1.0); 
    

    【讨论】:

      【解决方案4】:

      您可以使用图案颜色或描边图案 设置颜色模式:

      请确保图片为 ​​320x480 尺寸

      CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:myimage].CGColor);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 1970-01-01
        • 2021-06-21
        • 2016-06-03
        • 2011-03-31
        • 2013-06-13
        相关资源
        最近更新 更多