gpt4 book ai didi

cocoa - NSImage 变换后变得模糊

转载 作者:行者123 更新时间:2023-12-03 18:03:20 28 4
gpt4 key购买 nike

我使用 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”方法导致了这种情况,但我不知道如何在不使用该方法的情况下做到这一点。

最佳答案

我不确定它在这个管道中的位置,但是如果您可以获取NSGraphicsContext(这可能很简单:

NSGraphicsContext *myContext = [NSGraphicsContext currentContext];

在您lockFocus之后),您可以尝试

[myContext setImageInterpolation:NSImageInterpolationHigh]

要求 Cocoa 使用其最好的算法来缩放图像。如果您改用 CGImageRef API,我知道您可以轻松地更好地控制插值设置。

关于cocoa - NSImage 变换后变得模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4385644/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com