Proportional image resize

前端 未结 6 996
南笙
南笙 2020-12-07 14:51

I\'m having a little bit of a problem scaling my images to a properly predefined size. I was wondering - since it is purely mathematics, if there\'s some sort of common logi

6条回答
  •  抹茶落季
    2020-12-07 15:34

    Here is how I do it:

    + (NSSize) scaleHeight:(NSSize)origSize 
                 newHeight:(CGFloat)height {
    
        NSSize newSize = NSZeroSize;
        if ( origSize.height == 0 ) return newSize;
    
        newSize.height = height;
        CGFloat factor = ( height / origSize.height );
        newSize.width  = (origSize.width * factor );
    
        return newSize;
    }
    
    + (NSSize) scaleWidth:(NSSize)origSize 
                 newWidth:(CGFloat)width {
    
        NSSize newSize = NSZeroSize;
        if ( origSize.width == 0 ) return newSize;
    
        newSize.width  = width;
        CGFloat factor = ( width / origSize.width );
        newSize.height = (origSize.height * factor );
    
        return newSize;
    }
    

提交回复
热议问题