0

NSAffineTransform クラスを使用して NSImage をその中心で回転させたところ、動作しました。コードは次のとおりです。

- (NSImage*)rotateImage: (NSImage*)myImage angle:(float)rotateAngle {

    NSRect imageBounds = {NSZeroPoint, [myImage size]};
    NSRect transformedImageBounds = imageBounds;
    NSBezierPath* boundsPath = [NSBezierPath bezierPathWithRect:imageBounds];
    NSAffineTransform* transform = [NSAffineTransform transform];

    // calculate the bounds for the rotated image
    if (rotateAngle != 0) {

        NSAffineTransform* temptransform = [NSAffineTransform transform];
        [temptransform rotateByDegrees:rotateAngle];
        [boundsPath transformUsingAffineTransform:temptransform];

        NSRect rotatedBounds = {NSZeroPoint, [boundsPath bounds].size};

        // center the image within the rotated bounds
        imageBounds.origin.x += (NSWidth(rotatedBounds) - NSWidth (imageBounds))/2;
        imageBounds.origin.y += (NSHeight(rotatedBounds) - NSHeight (imageBounds))/2;

        // set up the rotation transform
        [transform translateXBy:+(NSWidth(rotatedBounds) / 2) yBy:+ (NSHeight(rotatedBounds) / 2)];
        [transform rotateByDegrees:rotateAngle];
        [transform translateXBy:-(NSWidth(rotatedBounds) / 2) yBy:- (NSHeight(rotatedBounds) / 2)];

        transformedImageBounds = rotatedBounds;
    }

    // draw the original image into the new image
    NSImage* transformedImage = [[NSImage alloc] initWithSize:transformedImageBounds.size];;
    [transformedImage lockFocus];
    [transform concat];
    [myImage drawInRect:imageBounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0] ;

    [transformedImage unlockFocus];

    return transformedImage;
}

しかし、結果の画像の品質は非常に悪いです。それを解決する方法は?

「lockFocus」メソッドが原因だと言う人もいますが、メソッドを使用せずにこれを行う方法がわかりません。

4

1 に答える 1

1

このパイプラインのどこにあるのかわかりませんが、NSGraphicsContextを取得できれば(次のように簡単かもしれません:

NSGraphicsContext *myContext = [NSGraphicsContext currentContext];

あなたのlockFocusの後)、あなたは試すことができます

[myContext setImageInterpolation:NSImageInterpolationHigh]

画像のスケーリングに最適なアルゴリズムを使用するよう Cocoa に依頼します。CGImageRef APIの使用に切り替えると、補間設定をより簡単に制御できることがわかります。

于 2010-12-08T13:56:26.000 に答える