[ios] Crop image to specified size and picture location

I have an image that is being taken using AVCaptureSession and being returned to my view controller.

Here is a screen shot of the camera interface:

enter image description here

The AVCaptureSession takes the FULL SCREEN photo, and now I want to crop it down to the size of the visible window above.

The window will always be at the same location and the same size.

Height = 360 (180 non retina)px, Width = 640 (320 non retina)px, x = 0, y = 160.

How can I crop the image down to these coordinates so that the image itself is now 360 x 640?

Any help would be great, thanks!

This question is related to ios uiimage crop avcapturesession

The answer is


You would need to do something like this. I am typing this off the top of my head, so this may not be 100% correct.

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, 640, 360, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst); CGColorSpaceRelease(colorSpace);  CGContextDrawImage(context, CGRectMake(0,-160,640,360), cgImgFromAVCaptureSession);  CGImageRef image = CGBitmapContextCreateImage(context); UIImage* myCroppedImg = [UIImage imageWithCGImage:image]; CGContextRelease(context);       

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to uiimage

Crop image to specified size and picture location Convert UIImage to NSData and convert back to UIImage in Swift? How can I color a UIImage in Swift? How to Resize image in Swift? How to set image for bar button with swift? How do you create a UIImage View Programmatically - Swift Navigation bar with UIImage for title Loading/Downloading image from URL on Swift How can I change image tintColor in iOS and WatchKit UIImageView aspect fit and center

Examples related to crop

Crop image to specified size and picture location How to "crop" a rectangular image into a square with CSS? How to crop an image using PIL? Android Crop Center of Bitmap How to Split Image Into Multiple Pieces in Python Crop image in PHP What's the algorithm to calculate aspect ratio?

Examples related to avcapturesession

Crop image to specified size and picture location Turn on torch/flash on iPhone