Split UIImage in half?

后端 未结 3 1433
情书的邮戳
情书的邮戳 2020-12-08 12:31

How would I go about splitting a UIImage In half(down the middle) so it would make two images?

3条回答
  •  [愿得一人]
    2020-12-08 12:48

    iOS 11, swift 4.0 updated version https://stackoverflow.com/users/893872/durul-dalkanat which I think to be honest is the best :)

    This splits an image into 4 parts

    func splitImage(image2D: UIImage) -> [UIImage] {
        let imgWidth = image2D.size.width / 2
        let imgHeight = image2D.size.height / 2
        var imgImages:[UIImage] = []
    
        let leftHigh = CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight)
        let rightHigh = CGRect(x: imgWidth, y: 0, width: imgHeight, height: imgHeight)
        let leftLow = CGRect(x: 0, y: imgHeight, width: imgWidth, height: imgHeight)
        let rightLow = CGRect(x: imgWidth, y: imgHeight, width: imgWidth, height: imgHeight)
    
        let leftQH = image2D.cgImage?.cropping(to:leftHigh)
        let rightHQ = image2D.cgImage?.cropping(to:rightHigh)
        let leftQL = image2D.cgImage?.cropping(to:leftLow)
        let rightQL = image2D.cgImage?.cropping(to:rightLow)
    
        imgImages.append(UIImage(cgImage: leftQH!))
        imgImages.append(UIImage(cgImage: rightHQ!))
        imgImages.append(UIImage(cgImage: leftQL!))
        imgImages.append(UIImage(cgImage: rightQL!))
    
        return imgImages
    }
    

提交回复
热议问题