Article Image
Article Image
View All Posts
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

I recently had to write some code to use the UIImagePickerControllerCropRect when picking or taking a photo. Looking around the web there were some pretty crazy coding examples that seemed to be unnecessarily complicated so I knocked up my own quick solution.

// ger the original image along with it's size
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
CGSize size = image.size;
// crop the crop rect that the user selected
CGRect cropRect = [[info objectForKey:UIImagePickerControllerCropRect] 

// create a graphics context of the correct size    
CGContextRef context = UIGraphicsGetCurrentContext();

// correct for image orientation    
UIImageOrientation orientation = [image imageOrientation];
if(orientation == UIImageOrientationUp) {
  CGContextTranslateCTM(context, 0, size.height);
  CGContextScaleCTM(context, 1, -1);                
  cropRect = CGRectMake(cropRect.origin.x, 
} else if(orientation == UIImageOrientationRight) {
  CGContextScaleCTM(context, 1.0, -1.0);
  CGContextRotateCTM(context, -M_PI/2);
  size = CGSizeMake(size.height, size.width);
  cropRect = CGRectMake(cropRect.origin.y, 
} else if(orientation == UIImageOrientationDown) {
  CGContextTranslateCTM(context, size.width, 0);
  CGContextScaleCTM(context, -1, 1);        
  cropRect = CGRectMake(-cropRect.origin.x, 
// draw the image in the correct place
CGContextTranslateCTM(context, -cropRect.origin.x, -cropRect.origin.y);
                   CGRectMake(0,0, size.width, size.height), 
// and pull out the cropped image
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();

Related Posts

Reading barcodes in iOS7 - Discover how to easily read 1D and 2D barcodes in iOS7 using AVCaptureMetadataOutput in this step-by-step tutorial with sample code.
Vision Kit and CoreML - This tutorial demonstrates the process of wiring up the iPhone's camera to CoreML with the help of Vision Kit, allowing developers to run machine learning models on camera input and build image classification applications.
Augmented reality on the iPhone with iOS4.0 - Implement updated AR iPhone code using iOS4.0 to access real-time camera streams with this detailed tutorial, featuring step-by-step instructions for creating an AVCaptureSession and configuring output and resolution settings.

Related Videos

Vision framework and CoreML - Witness the incredible fusion of Apple's Vision framework and Core ML in this object identification demo, and grab the code from GitHub to explore this remarkable project further.
Browser-Based Augmented Reality Sudoku Solver using TensorFlow and Image Processing - Learn how to build a Sudoku app using browser APIs and modern techniques, including image processing pipeline, TensorFlow-powered neural networks, and OCR to efficiently identify, extract, and solve Sudoku puzzles.
Augmented Reality iPhone Sudoku Grab - Unlock the power of real-time augmented reality capture with the latest Sudoku Grab version and delve into app development with step-by-step instructions in the featured article.
Sudoku Grab for the iPhone - Solve Sudoku puzzles in seconds with this incredible iPhone App that takes a photograph of the puzzle and solves it for you! Find it on the App Store and learn how to use it on our blog.
Uploading SPIFFS using - Learn the new method for uploading to the SPIFFS file system in the latest version of PlatformIO and quickly locate the hidden upload file system command with this helpful guide.
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


> Image


A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts