title: How to get smooth corner radius on a non square Objective C UIImageView
body: <p>I am trying to get a nice round corner on a UIImageView. I have found an implemented the following code, which works fine if the image is a square. The problem is that my imageView is a rectangular photo, sometimes portrait sometimes landscape. This code is clipping off the corners, but doesn't give a smooth curve when one side of the image is longer than the other. Any Ideas? Also is the float in setCornerRadius applied to the image as a percentage of the sides or just straight pixel count?</p>
  
  <pre><code>    // Get the Layer of any view
      CALayer * l = [imageView layer];
      [l setMasksToBounds:YES];
      [l setCornerRadius:100.0];
  </code></pre>
  
  <p>Here is the fix for all interested.</p>
  
  <p>Okay after some playing with it I figured out I needed to resize the imageView to fit the image. I implemented the following method to set the frame size.</p>
  
  <pre><code>- (CGRect)getScaleForFrameFromImage:(UIImage *)image
  {
      // get the bigger side of image to determine the shape then
      // get the percentage we need to scale to to trim imageViewFrame 
      // so it fits image and sits in the space dedicated in main view for the image
      float percentage;
      float newWidth;
      float newHeight;
  
      float w = image.size.width;
      float h = image.size.height;
      if (w &gt; h) {
          // landscape
          percentage  = 280 / w;
          newWidth = w * percentage;
          newHeight = h * percentage;;
      }
      else {
          percentage  = 208 / h;
          newWidth = w * percentage;
          newHeight = h * percentage;
      }
      int xOrigin = 20 + (280 - newWidth) / 2;
      CGRect newFrame = CGRectMake(xOrigin, 160, newWidth, newHeight);
      return newFrame;
  }
  </code></pre>
  
  <p>then in my viewWillAppear I did this</p>
  
  <pre><code>    // set the imageFrame size
      [imageView setFrame:[self getScaleForFrameFromImage:imageToDisplay]];
  
      // Use that image to put on the screen in imageView
      [imageView setImage:imageToDisplay];
  
      // Get the Layer of imageView
      CALayer * l = [imageView layer];
      [l setMasksToBounds:YES];
      [l setCornerRadius:10.0];
  </code></pre>