首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSImage在转型后变得模糊

NSImage在转型后变得模糊
EN

Stack Overflow用户
提问于 2010-12-08 08:52:08
回答 1查看 920关注 0票数 0

我用NSImage类在它的中心旋转了一个NSAffineTransform,它可以工作。守则如下:

代码语言:javascript
复制
- (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“方法造成了这种情况,但我不知道如何不使用该方法。

EN

回答 1

Stack Overflow用户

发布于 2010-12-08 13:56:26

我不知道它在这个管道中的位置是什么,但是如果您能够获得NSGraphicsContext (这可能很简单,就像:

NSGraphicsContext *myContext = NSGraphicsContext currentContext;

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

setImageInterpolation:NSImageInterpolationHigh

myContext

要求可可使用它的最佳算法来缩放图像。如果您切换到使用CGImageRef API,我知道您可以很容易地获得对插值设置的更多控制。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4385644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档