9

アンチエイリアシングなしでUIBezierPathsをレンダリングしてから、完全なピクセル表現を保持するためにPNGとして保存する必要があります(たとえば、JPEGで画像を台無しにしないでください)。UIBezierPathsをストロークする直前に、以下のCG関数を呼び出してみましたが、レンダリングされた画像に影響はないようです。パスは引き続きアンチエイリアシングでレンダリングされます(つまり、スムージングされます)。

CGContextSetShouldAntialias(c, NO);
CGContextSetAllowsAntialiasing(c, NO);
CGContextSetInterpolationQuality(c, kCGInterpolationNone);

どんなヒットでも大歓迎です。

4

2 に答える 2

16

これらのオプションを使用すると、アンチエイリアスがオフになります。左側はデフォルトのオプションです。右側、オプション付き。

ここに画像の説明を入力してください

UIViewサブクラスを使用している場合、これは簡単に制御できます。これは私のdrawRectです:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(context, NO);

    [[UIColor redColor] setStroke];
    UIBezierPath *path = [self myPath];
    [path stroke];
}

そして、画面をキャプチャするには、プログラムでスクリーンショットを撮る方法から

- (void)captureScreen
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(self.window.bounds.size);
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:[self screenShotFilename] atomically:YES];
}

を使用している場合、ドキュメントに記載されているように、CAShapeLayer画面上のアンチエイリアスを制御することはできないと思います。

形状はアンチエイリアス処理されて描画され、可能な場合は常に、解像度の独立性を維持するためにラスタライズされる前に画面スペースにマッピングされます。ただし、CoreImageフィルターなど、レイヤーまたはその祖先に適用される特定の種類の画像処理操作では、ローカル座標空間でラスタライズが強制される場合があります。

ただし、画面上のアンチエイリアスに関係なく、画面のスナップショットをアンチエイリアスしないようにする場合はCGContextSetShouldAntialias、をcaptureScreenルーチンに挿入できます。

- (void)captureScreen
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(self.window.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(context, NO);
    [self.window.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);
    [data writeToFile:[self screenShotFilename] atomically:YES];
}
于 2013-03-14T19:16:02.580 に答える
5

どこcから来たの?あなたが使用している描画サイクルとc同じものを指していると確信していますか? 上記のサンプルからはわかりにくいです。UIGraphicsGetCurrentContext()[UIBezierPath stroke]

設定しているのと同じコンテキストに描画していることを確認したい場合は、CGPathからをフェッチし、UIBezierPath直接描画します。

- (void)drawRect:(CGRect)rect {
  CGContextRef context = UIGraphicGetCurrentContext();
  CGPathRef path = [self.bezier CGPath];
  CGContextSetShouldAntialias(context, NO);
  CGContextAddPath(context, path);
  CGContextStrokePath(context);
}
于 2013-03-14T19:14:19.383 に答える