iOS:使用 .png 在 View 中着色

标签 ios xcode cgcontext brush touchesmoved

我有这个代码:

-(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;
}

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

最佳答案

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

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

图像操作.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;

希望这有帮助。

关于iOS:使用 .png 在 View 中着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802036/

相关文章:

ios - 导航栏标题 View 上的多行 UILabel

javascript - Appium错误:uncaughtException:无法读取null的属性“executeAtom”

xcode - 为什么我的应用程序(游戏)的 fps(30) 很低? (xcode Sprite swift )

iPhone 应用程序 - 为什么我的 CGContext 圆圈看起来像黑色方 block ?

xcode - OS X 上的 Swift : How to access the current CGContext for drawing in -[NSView drawRect:]

ios - 无法使用 iOS6.1 sdk 构建适用于 IOS 版本 1.7.0 的 Google Maps SDK

xcode - 如何检测 UITextField 中的阿拉伯字符?

xcode - 如何将swift库项目编译成.so文件?

image - 我正在尝试清除cgcontext上当前绘制的图像

ios - 无法在 Swift 框架中导入 Common Crypto