2010-12-08 4 views
0

NSAffineTransformクラスを中心にNSImageを回転させて動作させます。 コードはここにある:トランスフォーム後に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"メソッドがこれを引き起こしたと言う人もいますが、このメソッドを使用せずにこれを行う方法はありません。

答えて

1

は、私はそれは場所がこのパイプラインにされますかわからないんだけど、あなたは同じくらい簡単かもしれません(NSGraphicsContextを手に入れることができる場合のように:

NSGraphicsContext *あるMyContext = [NSGraphicsContext currentContext ];あなたのlockFocus

)、あなたは

を試すことができます

[あるMyContext setImageInterpolation:NSImageInterpolationHigh]

は、画像をスケーリングするための最良のアルゴリズムを使用してココアを依頼します。 CGImageRef APIに切り替えると、補間設定をより簡単に制御できるようになります。

関連する問題