2015-10-09 93 views
0

我正在使用以下函數來旋轉NSImage。它使用塊代碼爲了正確渲染視網膜。現在的問題是,它在非視網膜屏幕上呈現模糊。在非視網膜上使用塊代碼模糊NSImage旋轉

我試着檢查視網膜顯示器[NSWindow backingScaleFactor],然後在非視網膜顯示器上使用非阻擋方法,但是這種情況無法使用 - 如果用戶有視網膜Mac,但是正在向外部非視網膜顯示器顯示時,檢查backingScaleFactor會報告視網膜屏幕。後來我發現蘋果的開發者網站這個文件:

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

由於

回答

0

嘗試此

  • (NSImage中*)rotateImagedByDegrees:(CGFloat的)度圖像:(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; 
    

    } }

相關問題