3

画像(ブラシ)を取り、表示された画像に描画したい。その画像のアルファに影響を与えたいだけで、後でエクスポートする必要があります。

私が見てきたことから、ほとんどの指示は、実際にはうまくいかないコストのかかる操作にしか入りません。つまり、オフスクリーン コンテキストに描画し、マスクの CGImage を作成し、ブラシが適用されるたびに CGImageWithMask を作成することをお勧めします。

これを実行してコンテキストに描画するだけでも、iPhone ではかなり大雑把であるため、これにはコストがかかることは既にわかっています。

私がやりたいのは、UIImageView の UIImage を取得し、そのアルファ チャネルを直接操作することです。また、ピクセル単位ではなく、独自の柔らかさを持つ大きな (半径 20 ピクセル) ブラシを使用します。

4

1 に答える 1

6

これには UIImageView を使用しません。通常の UIView で十分です。

画像をレイヤーに入れるだけです

UIView *view = ...
view.layer.contents = (id)image.CGImage;

その後、レイヤーにマスクを追加して、画像の一部を透明にすることができます

CALayer *mask = [[CALayer alloc] init]
mask.contents = maskimage.CGImage;
view.layer.mask = mask;

プロジェクトのために、指で画像を明らかにするために使用できるbrush.pngがある場所で何かをしました...私の更新マスク機能がありました:

- (void)updateMask {

    const CGSize size = self.bounds.size;
    const size_t bitsPerComponent = 8;
    const size_t bytesPerRow = size.width; //1byte per pixel
    BOOL freshData = NO;
    if(NULL == _maskData || !CGSizeEqualToSize(size, _maskSize)) {
        _maskData = calloc(sizeof(char), bytesPerRow * size.height);
        _maskSize = size;
        freshData = YES;
    }

    //release the ref to the bitmat context so it doesn't get copied when we manipulate it later
    _maskLayer.contents = nil;
    //create a context to draw into the mask
    CGContextRef context = 
    CGBitmapContextCreate(_maskData, size.width, size.height, 
                          bitsPerComponent, bytesPerRow,
                          NULL,
                          kCGImageAlphaOnly);
    if(NULL == context) {
        LogDebug(@"Could not create the context");
        return;
    }

    if(freshData) {
        //fill with mask with alpha == 0, which means nothing gets revealed
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));    
    }

    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0f, -1.0f);

    //Draw all the points in the array into a mask
    for (NSValue* pointValue in _pointsToDraw)
    {
        CGPoint point;
        [pointValue getValue:&point];
        //LogDebug(@"location: %@", NSStringFromCGPoint(point));

        [self drawBrush:[_brush CGImage] at:point inContext:context];
    }
    [_pointsToDraw removeAllObjects];

    //extract an image from it
    CGImageRef newMask = CGBitmapContextCreateImage(context);

    //release the context
    CGContextRelease(context);

    //now update the mask layer
    _maskLayer.contents = (id)newMask;
    //self.layer.contents = (id)newMask;
    //and release the mask as it's retained by the layer
    CGImageRelease(newMask);
}
于 2011-04-27T16:10:19.707 に答える