NSImage становятся размытыми после трансформации - PullRequest
0 голосов
/ 08 декабря 2010

Я повернул NSImage в его центре с помощью класса NSAffineTransform, и он работает.Код здесь:

- (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", но я не знаю, как это сделать без использования метода.

1 Ответ

1 голос
/ 08 декабря 2010

Я не уверен, что это место в этом конвейере, но если вы можете получить NSGraphicsContext (который может быть так же просто, как

NSGraphicsContext * myContext = [NSGraphicsContext currentContext];

после вашего lockFocus ), вы можете попробовать

[myContext setImageInterpolation: NSImageInterpolationHigh]

попросить Какао использовать лучшие алгоритмы для масштабирования изображения. Если вы переключитесь на использование CGImageRef API, я знаю, что вы довольно легко получите больший контроль над настройкой интерполяции.

...