Thursday, February 20, 2014

Retina Image Resize NSImage

-(NSSize) getNormalImageSize:(NSSize) oneSize
{
    CGFloat screenScale = [[NSScreen mainScreen] backingScaleFactor];
    float targetScaledWidth =oneSize.width/screenScale;
    float targetScaledHeight = oneSize.height/screenScale;
  
    return  NSMakeSize(targetScaledWidth, targetScaledHeight);
}

- (NSImage*)resize:(NSSize)destSize
{
    NSSize  oldSize=destSize;
    destSize=[self getNormalImageSize:destSize];
    NSAssert([[self representations] count], @"Bad Input Image...");

    NSImage *newImage = [[[NSImage alloc]initWithSize:destSize] autorelease];
    [newImage lockFocus];
    [self drawInRect:NSMakeRect(0, 0, destSize.width, destSize.height)
            fromRect:NSMakeRect(0, 0, self.size.width, self.size.height)
           operation:NSCompositeSourceOver fraction:1.0];
    [newImage unlockFocus];
  
    [newImage setSize:oldSize];
    return newImage;
}


No comments:

Post a Comment