3

私は次のようにプログラムで UIImage に描画しようとしています:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(2, 2), YES, 1);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // These three lines of code apparently do nothing.
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextSetAllowsAntialiasing(context, false);
    CGContextSetShouldAntialias(context, false);

    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:0 blue:0 alpha:1].CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
}

このコードは、明らかにアンチエイリアシング/補間の積極的な試みにより、結果の画像が非常にソフトになることを除けば機能します。最近傍補間を使用して画像をスケーリングする必要があります。このアンチエイリアシングを防ぐにはどうすればよいですか?

ありがとうございました!

4

1 に答える 1

3

コンテキストに描画する画像はアンチエイリアシングを使用しませんが (実際に描画している特定の画像に違いはありませんが)、画像ビューからデフォルトの補間動作を取得します。

これを変更するには、ビューのレイヤーのmagnificationFilter/プロパティを調整します。minificationFilter

self.imageView.layer.magnificationFilter = kCAFilterNearest;
self.imageView.layer.minificationFilter = kCAFilterNearest;

(これを機能させるには、QuartzCore フレームワークを追加する必要があります。)

于 2013-05-14T20:31:50.253 に答える