NSImage 变换后变得模糊

发布于 2024-10-06 13:40:04 字数 1779 浏览 4 评论 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”方法导致了这种情况,但我不知道如何在不使用该方法的情况下做到这一点。

I rotated a NSImage at its center with the NSAffineTransform class and it works.
The code is here:

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

But the quality of the resulted image is very bad.
How to solve it?

Some say the "lockFocus" methods caused this but I hv no idea of how to do this without using the method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2024-10-13 13:40:04

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

NSGraphicsContext *myContext =
[NSGraphicsContext currentContext];

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

[myContext setImageInterpolation:NSImageInterpolationHigh]

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

I'm not sure what it's place is in this pipeline, but if you can get hold of the NSGraphicsContext (which might be as easy as:

NSGraphicsContext *myContext =
[NSGraphicsContext currentContext];

after your lockFocus), you can try

[myContext setImageInterpolation:NSImageInterpolationHigh]

to ask Cocoa to use its best algorithms for scaling the image. If you switch to using the CGImageRef API, I know you can get more control over the interpolation setting pretty easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文