2012-01-20 8 views
2

私はCGPath(実際にはCGMutablePath)として定義された閉じたパスを持っています。CGPath内部に色を焼き付ける

パスで定義されたUIImageの特定の領域を焼きたいです。私はCore Graphicsを使いこなしていましたが、現在のところ、私の唯一の選択肢は、パスではなくCGRectで定義された長方形の領域に色を付けることです。

誰でも正しい方向に向けることができますか?私はそれがUIImageviewに正しく表示させるためにカメラからの90度を持った画像を回転させることに成功しロブの助けを使用して

--update

..

私が残した唯一の問題はあります私が描く必要があるPATHはまだ90度であり、スケールから外れています。現在使用しているコードは、次のとおりです。

- (UIImage*)applyColorFillWithPath:(CGMutablePathRef) path withColor:(UIColor*)color { 

CGFloat targetWidth = self.size.width; 
CGFloat targetHeight = self.size.height; 
CGImageRef imageRef = [self CGImage]; 
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 

if (bitmapInfo == kCGImageAlphaNone) { 
    bitmapInfo = kCGImageAlphaNoneSkipLast; 
} 

CGContextRef context; 
if (self.imageOrientation == UIImageOrientationUp || self.imageOrientation == UIImageOrientationDown) { 
    //UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight)); 
    context = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

} else { 
    //UIGraphicsBeginImageContext(CGSizeMake(targetHeight, targetWidth)); 
    context = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

} 

// set the fill color 
//[color setFill]; 

if (self.imageOrientation == UIImageOrientationLeft) { 
    CGContextRotateCTM(context, radians(90)); 
    CGContextTranslateCTM (context, 0, -targetHeight); 

} else if (self.imageOrientation == UIImageOrientationRight) { 
    CGContextRotateCTM (context, radians(-90)); 
    CGContextTranslateCTM (context, -targetWidth, 0); 

} else if (self.imageOrientation == UIImageOrientationUp) { 
    // NOTHING 
} else if (self.imageOrientation == UIImageOrientationDown) { 
    CGContextTranslateCTM (context, targetWidth, targetHeight); 
    CGContextRotateCTM (context, radians(-180)); 
} 

CGContextDrawImage(context, CGRectMake(0, 0, targetWidth, targetHeight),imageRef); 

CGContextBeginPath(context); 
CGContextAddPath(context, path); 
CGContextSaveGState(context); 
CGContextClip(context); 
CGContextSetBlendMode(context, kCGBlendModeNormal); 
CGContextSetFillColorWithColor(context, color.CGColor); 
CGContextFillRect(context, CGPathGetBoundingBox(path)); 
CGContextRestoreGState(context); 

// Turn the bitmap context into a UIImage. 
CGImageRef cgImage = CGBitmapContextCreateImage(context); 
CGContextRelease(context); 
UIImage *coloredImg = [UIImage imageWithCGImage:cgImage scale:1 orientation:UIImageOrientationUp]; 
CGImageRelease(cgImage); 

//return the color-burned image 
return coloredImg; 
} 

次の画像が結果です。赤い矩形はCGPATHの表現です。白い四角は、実際にはパス上の上記のメソッドの結果です。

http://i41.tinypic.com/f1zbdi.png

私は手動でCOSの数学者とものではありません..私は何かを監督かもしれませんがを使用してCGPathに90度回転する必要があると..?

答えて

2

影響を受けるピクセルを制限するために、コンテキストのクリッピングパスをパスに設定します。

CGContextRef gc = myGraphicsContext(); 
CGMutablePathRef path = myMutablePathRef(); 

CGContextBeginPath(gc); 
CGContextAddPath(gc, path); 
CGContextSaveGState(gc); { 
    CGContextClip(gc); 

    // Do color burn. For example: 
    CGContextSetBlendMode(gc, kCGBlendModeColorBurn); 
    CGContextSetFillColorWithColor(gc, [UIColor redColor].CGColor); 
    CGContextFillRect(gc, CGPathGetBoundingBox(path)); 
} CGContextRestoreGState(gc); 
+0

ありがとうRob!あなたは本当にここで私を助けました。基本的に私はそれが難しいと思っていました。マスクを作成しようとしていて、内容をコピーして、それを色づけして元の画像にマージしました... –

+0

私が残した奇妙なことは、カラーバーンを適用しながら画像を90°回転させます。風景や何かに自動回転するように見えます。 UIImageは、ポートレートモードで撮影された画像ピッカーからの写真です。 Imageviewモードを「拡大縮小」に設定しました。 –

+0

http://stackoverflow.com/questions/1260249/resizing-uiimages-pulled-from-the-camera-also-rotates-the-imimage –

関連する問題