在非视网膜上使用块码模糊的NSImage旋转

时间:2015-10-09 01:44:28

标签: macos retina-display core-image image-rotation nsimage

我使用以下函数来旋转NSImage。它使用块代码以便在视网膜上正确渲染。现在的问题是它在非视网膜屏幕上呈现模糊。

我尝试检查视网膜显示器[NSWindow backingScaleFactor],然后在非视网膜显示器上使用非阻滞方法,但是如果用户有视网膜Mac就不能使用它但是显示到外部非视网膜显示器,检查支持ScaleFactor报告视网膜屏幕。然后我在Apple的开发者网站上找到了这个文档:

支持高分辨率的API https://developer.apple.com/library/mac/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/APIs/APIs.html

但坦率地说,这是不可理喻的。如何修改以下功能,以便在非视网膜显示屏上不模糊?

- (NSImage *)imageRotatedByDegrees:(CGFloat)degrees {
// calculate the bounds for the rotated image
NSRect imageBounds = {NSZeroPoint, [self size]};
NSBezierPath *boundsPath = [NSBezierPath bezierPathWithRect:imageBounds];
NSAffineTransform *transform = [NSAffineTransform transform];

[transform rotateByDegrees:degrees];
[boundsPath transformUsingAffineTransform:transform];

NSRect rotatedBounds = {NSZeroPoint, [boundsPath bounds].size};

// center the image within the rotated bounds
imageBounds.origin.x = NSMidX(rotatedBounds) - (NSWidth (imageBounds) / 2);
imageBounds.origin.y = NSMidY(rotatedBounds) - (NSHeight (imageBounds) / 2);

NSImage *rotatedImage = [NSImage imageWithSize:rotatedBounds.size flipped:NO drawingHandler:^BOOL (NSRect dstRect) {
    // set up the rotation transform
    NSAffineTransform *transform=[NSAffineTransform transform];
    [transform translateXBy:+(NSWidth(rotatedBounds) / 2) yBy:+(NSHeight(rotatedBounds) / 2)];
    [transform rotateByDegrees:degrees];
    [transform translateXBy:-(NSWidth(rotatedBounds) / 2) yBy:-(NSHeight(rotatedBounds) / 2)];

    // draw the original image, rotated, into the new image
    [transform concat];
    [self drawInRect:imageBounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
    return YES;
}];

return rotatedImage;
}

由于

1 个答案:

答案 0 :(得分:0)

试试这个

  • (NSImage *)rotateImagedByDegrees:(CGFloat)degrees图像:(NSImage *)sourceImage {

    NSImage *image = sourceImage;
    
    if (degrees == 0)
    {
        return image;
    }
    else
    {
    
        NSRect imageBounds = {NSZeroPoint, [sourceImage size]};
        NSSize rotatedSize = NSMakeSize(sourceImage.size.height, sourceImage.size.width) ;
        NSImage* rotatedImage = [[NSImage alloc] initWithSize:rotatedSize] ;
    
        NSBezierPath* boundsPath = [NSBezierPath bezierPathWithRect:imageBounds];
        NSAffineTransform* transform = [NSAffineTransform transform];
        [transform rotateByDegrees:-1.0 * degrees];
        [boundsPath transformUsingAffineTransform:transform];
        NSRect rotatedBounds = {NSZeroPoint, [boundsPath bounds].size};
        imageBounds.origin.x = NSMidX(rotatedBounds) - (NSWidth(imageBounds) / 2);
        imageBounds.origin.y = NSMidY(rotatedBounds) - (NSHeight(imageBounds) / 2);
    
        rotatedImage = [NSImage imageWithSize:rotatedBounds.size flipped:NO drawingHandler:NO];
    
        NSAffineTransform* transform1 = [NSAffineTransform transform];
        [transform1 translateXBy:+(NSWidth(rotatedBounds) / 2) yBy:+(NSHeight(rotatedBounds) / 2)];
        [transform1 rotateByDegrees:-1.0 * degrees];
        [transform1 translateXBy:-(NSWidth(rotatedBounds) / 2) yBy:-(NSHeight(rotatedBounds) / 2)];
        [rotatedImage lockFocus] ;
        [transform1 concat] ;
    
        [sourceImage drawInRect:imageBounds
                       fromRect:NSMakeRect(0.0, 0.0, [sourceImage size].width, [sourceImage size].height)
                      operation:NSCompositeSourceOver fraction:1.0];
    
        [rotatedImage unlockFocus] ;
        return rotatedImage;
    

    } }