0

I have been able to get the behavior I want using Xcode and the Attributes Inspector. I have also found a lot of really good information here on stackoverflow. The problem I cannot resolve is that cropping an image in code uses pixels but the rect I have is in points. Help!

This is what I am trying to do: I have an image that is full size on the iPhone. I want to capture a portion of the center of the image. But I want that portion to be the size of my rectangle.

In Xcode, I created a view with a uiimageview with the uiimageview view mode = center and Clip Subviews checked. Then, in code I set the image. Voila! My image is cropped at the center and displayed in the rectangular shaped view.

Can someone please help me do this in code? I've been trying different approaches for days....feeling pretty dumb by now.

Thank you in advance for your help.

Patricia
  • 5,019
  • 14
  • 72
  • 152
  • I found a pixel to points converter on GitHub: https://gist.github.com/jordiboehmelopez/3168819 I was able to use that logic to create a points to pixel converter. I'll post my answer if it works. – Patricia Jul 23 '13 at 20:29
  • points to pixel converter; close but no cigar: - (CGFloat)pointsToPixel:(CGFloat)pts { CGFloat pointsPerInch = 72.0; CGFloat scale = [[UIScreen mainScreen] scale]; float pixelPerInch; // aka dpi if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { pixelPerInch = 132 * scale; } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { pixelPerInch = 163 * scale; } else { pixelPerInch = 160 * scale; } CGFloat inches = pts / pointsPerInch; CGFloat pixels = inches * pixelPerInch; return pixels; } – Patricia Jul 25 '13 at 17:02
  • The code I posted appears to work but it's off and I don't know why. My pts passed in are 100 and it returns 452 but I need 600. That's the points for the height. My pts passed in for width are 150 and it returns 679 but I need 900. So...not sure how to get the values that I need. – Patricia Jul 25 '13 at 17:09
  • I did find away to crop the image in code just like I did in Xcode by setting the view.frame property to the correct size and setting the view.contentMode property to UIViewContentModeCenter and then setting the view.clipsToBounds = YES. – Patricia Jul 25 '13 at 17:14
  • But I'm still trying to figure out how to grab the cropped image to save it. – Patricia Jul 25 '13 at 17:15
  • You may refer this question: http://stackoverflow.com/questions/158914/cropping-a-uiimage – liruqi Oct 29 '13 at 05:06

0 Answers0