8

I am working on the custom camera application. Things are working good.

But I got into the problem while cropping the image from bottom. i.e. cropped image is having exact same width as the original image but height will be 1/3 of the original image and it must start from the bottom.

For Example Like this is the full image I need the red part only

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
BornShell
  • 381
  • 3
  • 14

4 Answers4

26

Swift 3 solution:

func cropBottomImage(image: UIImage) -> UIImage {
    let height = CGFloat(image.size.height / 3)
    let rect = CGRect(x: 0, y: image.size.height - height, width: image.size.width, height: height)
    return cropImage(image: image, toRect: rect)
}

Helper method for cropping with a rect:

func cropImage(image:UIImage, toRect rect:CGRect) -> UIImage{
    let imageRef:CGImage = image.cgImage!.cropping(to: rect)!
    let croppedImage:UIImage = UIImage(cgImage:imageRef)
    return croppedImage
}
alexburtnik
  • 7,661
  • 4
  • 32
  • 70
9

this is almost Alexburtnik's answer

but just to mention that UIImage.size is the logical size (in "points")

however, CGImage.cropping() used the actual dimension (in "pixels")

therefore, if you use a image with @2x or @3x modifiers, you will find the cropping is actually half or one third of the expectation.

so when you crop, you may consider multiplying the rect by the "scale" property of the image first, like the following:

func cropImage(image:UIImage, toRect rect:CGRect) -> UIImage? {
    var rect = rect
    rect.size.width = rect.width * image.scale
    rect.size.height = rect.height * image.scale

    guard let imageRef = image.cgImage?.cropping(to: rect) else {
        return nil
    }

    let croppedImage = UIImage(cgImage:imageRef)
    return croppedImage
}
royl8
  • 331
  • 5
  • 6
1

As an extension to UIImage:

import UIKit

extension UIImage {
    func crop(to rect: CGRect) -> UIImage? {
        // Modify the rect based on the scale of the image
        var rect = rect
        rect.size.width = rect.size.width * self.scale
        rect.size.height = rect.size.height * self.scale

        // Crop the image
        guard let imageRef = self.cgImage?.cropping(to: rect) else {
            return nil
        }

        return UIImage(cgImage: imageRef)
    }
}

Usage:

let myImage = UIImage(named:"myImage.png")    
let croppedRect = CGRect(x: 0, y: 0, width: newWidth, height: newHeight)
let croppedImage = myImage.crop(to: croppedRect)
jackofallcode
  • 1,935
  • 1
  • 13
  • 15
0

Method :

func croppIngimage(ImageCrop:UIImage, toRect rect:CGRect) -> UIImage{

    var imageRef:CGImageRef = CGImageCreateWithImageInRect(imageToCrop.CGImage, rect)
    var croppedimage:UIImage = UIImage(CGImage:imageRef)
    return croppedimage
}

Call :

var ImageCrop:UIImage  = UIImage(named:"image.png")
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
  • Suppose i have a image size of 2448 × 3264 i want that portion i.e. red one. What are the toRect Parameters ???? – BornShell Oct 11 '16 at 06:57
  • See this : http://stackoverflow.com/questions/37946990/cgrectmake-cgpointmake-cgsizemake-cgrectzero-cgpointzero-is-unavailable-in – Jamshed Alam Oct 11 '16 at 07:10