15

I'm writing an app that takes an image and crops out everything except for a rectangle in the center of the image. (SWIFT) I can't get the crop function to work though. This is what I have now:

func cropImageToBars(image: UIImage) -> UIImage {
     let crop = CGRectMake(0, 200, image.size.width, 50)

     let cgImage = CGImageCreateWithImageInRect(image.CGImage, crop)
     let result: UIImage = UIImage(CGImage: cgImage!, scale: 0, orientation: image.imageOrientation)

     UIImageWriteToSavedPhotosAlbum(result, self, nil, nil)

     return result
  }

I've looked at a lot of different guides but none of them seem to work for me. Sometimes the image is rotated 90 degrees, I have no idea why it does that.

mawnch
  • 385
  • 2
  • 4
  • 13
  • Welcome to stack OverFlow. Before your question, please check what helpful on this site is available. http://stackoverflow.com/questions/158914/cropping-an-uiimage/29294333#29294333 – pedrouan Sep 03 '16 at 19:49
  • Looks like a similar question, but I'm having a problem with Swift, not Objective-C. – mawnch Sep 03 '16 at 19:51
  • And there are many solutions for swift as well. This looks the best: http://stackoverflow.com/a/30403863/661022 – pedrouan Sep 03 '16 at 19:52
  • Thanks for answering. How do I implement an extension in Swift? I assume you just add it to the end of say, your main view controller class? – mawnch Sep 03 '16 at 19:56
  • 2
    This question has more and better answer than the other. It would be fair to post "This question already has answers here:" in the other question. – Saran Jun 25 '20 at 15:07

1 Answers1

29

If you would like to use an extension, just simply add it to the file, in the beginning, or the end. You can create an extra file for such code.

Swift 3.0

extension UIImage {
    func crop( rect: CGRect) -> UIImage {
        var rect = rect
        rect.origin.x*=self.scale
        rect.origin.y*=self.scale
        rect.size.width*=self.scale
        rect.size.height*=self.scale

        let imageRef = self.cgImage!.cropping(to: rect)
        let image = UIImage(cgImage: imageRef!, scale: self.scale, orientation: self.imageOrientation)
        return image
    }
}


let myImage = UIImage(named: "Name")
myImage?.crop(rect: CGRect(x: 0, y: 0, width: 50, height: 50))

for crop of a center portion of an image:

let imageWidth = 100.0
let imageHeight = 100.0
let width = 50.0
let height = 50.0
let origin = CGPoint(x: (imageWidth - width)/2, y: (imageHeight - height)/2)
let size = CGSize(width: width, height: height)

myImage?.crop(rect: CGRect(origin: origin, size: size))
pedrouan
  • 12,762
  • 3
  • 58
  • 74
  • I posted the example in latest Swift 3.0. In case you work in the older version, copy paste will require some corrections. – pedrouan Sep 03 '16 at 20:04
  • The cropping seems to work but I can't get it to crop the section of the image that I actually want. For example, arguments (50,50,500,500) returns a portion of the image from the top right of the screen, which makes no sense to me. If I want a rectangle in the middle of the screen (sort of like this http://imgur.com/a/vOMsb) what should the rectangle args be? – mawnch Sep 03 '16 at 20:17
  • @KaviRamamurthy I've updated my answer. – pedrouan Sep 03 '16 at 20:38
  • I tried it and it returns a rotated image. Any way to rotate it back? – mawnch Sep 03 '16 at 20:47
  • I haven't tested. Rotated? Weird. Here you can rotate your image. But I am not sure if I understand you. http://stackoverflow.com/a/38678800/661022 – pedrouan Sep 04 '16 at 14:50
  • I have also struggled with this. The image only seems to be cropping its upper coordinates. – Mehdi Jun 27 '17 at 20:54
  • I believe I have the same problem @MehdiAbderezai has; After cropping, the image's center shifts to the top left. – Kelvin Lau Dec 02 '17 at 03:27
  • What if the UIImage by chance does not contain a CGImage, and instead a CIImage? – Chewie The Chorkie Jan 14 '20 at 17:49