title: Image cropping issue in iOS
body: <p>I am creating an iPhone app which has image cropping feature. In this, I am getting the photos from the UIImagePickerController and passing it for cropping. There it has a scrollview and the selected image will be added as a subview to the scrollview. And I am using a UIButton for selecting the area for cropping. User can move the button over the imageview and place it anywhere, and when click on CROP button, the area similar to the frame size of the button should be cropped from the imageview.</p>
  
  <p>I used the following code, but it is not returning the actual image.</p>
  
  <pre><code>CGRect clippedRect  = CGRectMake(self.scrollView.frame.origin.x+90, self.scrollView.frame.origin.y, self.scrollView.frame.size.width-180, self.scrollView.frame.size.height-220);
  CGImageRef imageRef = CGImageCreateWithImageInRect([self.myPhoto CGImage], clippedRect);
  UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
  CGImageRelease(imageRef);
  
  self.imageView.image = newImage;
  </code></pre>
  
  <p>also used</p>
  
  <pre><code>- (UIImage *)cropImage:(UIImage *)oldImage {
      CGSize imageSize = self.cropFrame.frame.size;
      UIGraphicsBeginImageContextWithOptions( CGSizeMake( imageSize.width, imageSize.height), NO, 0.);
      [oldImage drawAtPoint:CGPointMake( xPosition, yPosition)
                  blendMode:kCGBlendModeCopy
                      alpha:1.];
      UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return croppedImage;
  }
  </code></pre>
  
  <p>but the result image is not the exact image as per the button frame. I am getting the image from another area.</p>
  
  <p><strong>Updated code</strong></p>
  
  <pre><code>- (void)loadPhoto{
  
      CGFloat w = self.myPhoto.size.width;
      CGFloat h = self.myPhoto.size.height;
      CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
      self.scrollView.contentSize = imageViewFrame.size;
  
      UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
      iv.contentMode = UIViewContentModeScaleAspectFit;
      iv.image = self.myPhoto;
      [self.view addSubview:iv];
      self.imageView = iv;
      [iv release];    
  }
  
  CGRect crop;//= CGRectMake(10, 10, 360, 360);
  crop.origin.x = self.cropFrame.frame.origin.x;
  crop.origin.y = self.cropFrame.frame.origin.y;
  crop.size.width = roundf(self.cropFrame.frame.size.width * 2.0f); //self.cropFrame.frame.size.width * 2;
  crop.size.height = roundf(self.cropFrame.frame.size.height * 2.0f);  //self.cropFrame.frame.size.height * 2;
  
  NSLog(@"Rect: %@", NSStringFromCGRect(crop));
  self.imageView.image = [self croppedImage:crop];
  
  - (UIImage *)croppedImage:(CGRect)bounds {
      CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageView.image CGImage], bounds);
      UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:self.myPhoto.imageOrientation];
      CGImageRelease(imageRef);
      return croppedImage;
  }
  </code></pre>
  
  <p>Please help to find a solution.</p>