2012-03-21 58 views
2

我有這樣的代碼:IOS:使用巴紐顏色在視圖

-(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到顏色(如例如刷子);並有特別的效果;我應該做什麼改變?

回答

0

如果你的位圖的png只有單一的顏色,那麼你可以設置該rgb的值。如果你想繪製png本身就像路徑使用相同的技術用於繪製線使用points..Set繼續放置點wil給你的線嘗試通過放置PNG圖像來實現這一點。 我的意思是在這裏你的單個PNG表現爲點。

+0

您可以發表我舉個例子? – CrazyDev 2012-03-21 10:11:42

+0

我希望我可以幫你ü智慧一些樣本..我從來沒有嘗試過,但這是可能的解決方案進入我的腦海,第一次看.. – Allamaprabhu 2012-03-21 10:19:36

0
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); 
2

我寫了一個方法來獲得從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; 

希望這有助於。

+0

不,你不明白我的問題;我想在一個視圖中的顏色,就好像我使用畫筆而不是一個簡單的點! – CrazyDev 2012-03-21 14:40:46

+0

你的問題非常模糊,沒有理由生氣。我的理解是,你試圖從一個PNG(圖像)獲取顏色,並使用該顏色來設置UIView的背景顏色。如果這不正確,請編輯您的問題。 – 2012-03-21 15:49:33

+0

我發佈了一個例子,說明如何用簡單的線條進行着色,然後我想用png着色 – CrazyDev 2012-03-21 16:49:35

0

可以使用圖案顏色或敲擊圖案 設置顏色模式:

請確保該圖像是規模小320x480

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:myimage].CGColor); 
相關問題