Image scaling and Basic C

Discussion in 'iOS Programming' started by nashyo, Jul 14, 2012.

  1. nashyo, Jul 14, 2012
    Last edited: Jul 14, 2012

    nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    Can anyone help me with this please.

    Code:
    -(UIImage*)resizedImageWithBounds:(CGSize)bounds
    {
        CGFloat horizontalRatio = bounds.width / self.size.width;
        CGFloat verticalRatio = bounds.height / self.size.height;
        CGFloat ratio = MIN(horizontalRatio, verticalRatio);
        CGSize newSize = CGSizeMake(self.size.width * ratio, self.size.height * ratio);
        
        UIGraphicsBeginImageContextWithOptions(newSize, YES, 0);
        [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return newImage;
    }
    What does MIN() do?
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I tried using it in my app and it wasn't recognized, so it doesn't seem to be part of a standard library/framework.

    It appears to be a macro, based on the decision to name it in all capitals. (I'll be honest, Xcode suggested it to me. I already suspected it was a macro, but it didn't even occur to me that there might be a naming convention... I've always just used whatever names I feel like for macros.)

    If I had to guess, I'd say that somewhere in a header you import (or possibly you import a header that imports it... or possibly so on) you'll probably find something like

    Code:
    #define MIN(A,B) (A<B)?A:B
    That's just a guess though. The macro I just typed would replace every instance of MIN(A,B) with a ternary operator that first checks if A is less than B, and if it is, return A, and if it isn't, return B. In effect, it returns the lesser of A and B, or finds the MINimum (my guess for what MIN is short for) value of A and B.
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    Thanks. Its difficult to search for an answer to this.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Where did the code come from?
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    MIN and MAX are a part of standard Objective-C. A quick check shows that they are macros defined in NSObjeCRuntime.h, which is almost certainly #imported in every .m file.

    MIN returns the minimum value of the two parameters and MAX returns the maximum value of the two parameters.
     

Share This Page