【问题标题】:NSImage become blurry after transformationNSImage 转换后变得模糊
【发布时间】:2010-12-08 08:52:08
【问题描述】:

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

【问题讨论】:

    标签: cocoa nsimage


    【解决方案1】:

    我不确定它在这个管道中的位置,但如果你能掌握 NSGraphicsContext(这可能很简单:

    NSGraphicsContext *myContext = [NSGraphicsContext currentContext];

    在你的lockFocus之后,你可以试试

    [myContext setImageInterpolation:NSImageInterpolationHigh]

    要求 Cocoa 使用其最佳算法来缩放图像。如果你改用 CGImageRef API,我知道你可以很容易地控制插值设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2014-11-28
      • 2014-11-23
      • 2014-12-08
      • 2018-07-20
      • 2017-04-19
      相关资源
      最近更新 更多