4

I am working with Swift 2.0 and I would like to split an image into multiple pieces. I know this is a repeat, but they syntax is old and I am having trouble update.

user4812000
  • 1,033
  • 1
  • 13
  • 24
  • what did you done so far? any code? what do you mean under `split`? also check: http://stackoverflow.com/questions/158914/cropping-a-uiimage/25824454#25824454 – Maxim Shoustin Oct 12 '15 at 21:56
  • I am trying to learn this before I start my project, but this is the outdated code I have found: UIImage* subimage = [originalImage subimageWithRect:CGRectMake(0, 0, 100, 100)]; – user4812000 Oct 12 '15 at 21:56
  • Add additional code and information to the question. What you have is a method call, where did it come from? – zaph Oct 12 '15 at 21:59
  • 1
    Duplicate of the SO question: [how to crop image in to pieces programmatically](http://stackoverflow.com/q/4743242/451475). – zaph Oct 12 '15 at 22:00
  • This is exactly what I am looking for, but I am having trouble updating the syntax of the method: http://stackoverflow.com/questions/6014953/how-to-split-an-image-in-to-multiple-parts – user4812000 Oct 12 '15 at 22:03
  • As I stated, add *your* code along with what the problem is to the question. SO is not a code writing service, it is here to help developers with their code. – zaph Oct 12 '15 at 22:07
  • As I stated, my code will not be helpful in the slightest. If you had read you question, you would realize I am asking for help updating the syntax from a previously asked question not asking for someone to write my application. – user4812000 Oct 12 '15 at 22:10
  • The answer in the question has the code. – zaph Oct 12 '15 at 22:36
  • Read the third comment in this chain again. – user4812000 Oct 12 '15 at 22:46

1 Answers1

17

update: Xcode 8.2.1 • Swift 3.0.2

You can add this extension to split your images:

extension UIImage {
    var topHalf: UIImage? {
        cgImage?.cropping(
            to: CGRect(
                origin: .zero,
                size: CGSize(width: size.width, height: size.height / 2)
            )
        )?.image
    }
    var bottomHalf: UIImage? {
        cgImage?.cropping(
            to: CGRect(
                origin: CGPoint(x: .zero, y: size.height - (size.height/2).rounded()),
                size: CGSize(width: size.width, height: size.height - (size.height/2).rounded())
            )
        )?.image
    }
    var leftHalf: UIImage? {
        cgImage?.cropping(
            to: CGRect(
                origin: .zero,
                size: CGSize(width: size.width/2, height: size.height)
            )
        )?.image
    }
    var rightHalf: UIImage? {
        cgImage?.cropping(
            to: CGRect(
                origin: CGPoint(x: size.width - (size.width/2).rounded(), y: .zero),
                size: CGSize(width: size.width - (size.width/2).rounded(), height: size.height)
            )
        )?.image
    }
}

extension CGImage {
    var image: UIImage { .init(cgImage: self) }
}

Playground testing:

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

let imgURL = URL(string: "https://i.stack.imgur.com/Xs4RX.jpg")!
URLSession.shared.dataTask(with: imgURL) { data, response, error in
    guard
        let data = data, let image = UIImage(data: data)
    else { return }
    
    let topHalf = image.topHalf
    let bottomHalf = image.bottomHalf
    
    let topLeft = topHalf?.leftHalf
    let topRight = topHalf?.rightHalf
    let bottomLeft = bottomHalf?.leftHalf
    let bottomRight = bottomHalf?.rightHalf
}.resume()
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571