2010-12-08 6 views
0

J'ai fait pivoter un NSImage en son centre avec la classe NSAffineTransform et cela fonctionne. Le code est ici:NSImage devient flou après la transfomation

- (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; 

}

Mais la qualité de l'image résultat est très mauvais. Est-ce que quelqu'un sait comment le résoudre?

Certains disent que les méthodes "lockFocus" ont causé cela mais je n'ai aucune idée de la façon de le faire sans utiliser la méthode.

Répondre

1

Je ne suis pas sûr de ce que sa place est dans ce pipeline, mais si vous pouvez mettre la main sur la NSGraphicsContext (qui pourrait être aussi simple que:

NSGraphicsContext * myContext = [NSGraphicsContext CurrentContext ];

après votre lockFocus), vous pouvez essayer

[myContext setImageInterpolation: NSImageInterpolationHigh]

demander Cocoa d'utiliser ses meilleurs algorithmes pour l'image mise à l'échelle. Si vous passez à l'utilisation de l'API CGImageRef, je sais que vous pouvez contrôler plus facilement le paramètre d'interpolation.

Questions connexes