Divide image in array of images with swift

前端 未结 3 1324
迷失自我
迷失自我 2021-01-03 02:33

I\'m trying to divide an image to create 16 out of it (in a matrix). I\'m using swift 2.1. Here\'s the code:

let cellSize = Int(originalImage.size.height) /          


        
3条回答
  •  孤街浪徒
    2021-01-03 03:21

    you can split your image im two parts vertically and horizontally and sub split the result as needed:

    extension UIImage {
        var topHalf: UIImage? {
            guard let cgImage = cgImage, let image = cgImage.cropping(to: CGRect(origin: .zero, size: CGSize(width: size.width, height: size.height/2))) else { return nil }
            return UIImage(cgImage: image, scale: scale, orientation: imageOrientation)
        }
        var bottomHalf: UIImage? {
            guard let cgImage = cgImage, let image = cgImage.cropping(to: CGRect(origin: CGPoint(x: 0,  y: CGFloat(Int(size.height)-Int(size.height/2))), size: CGSize(width: size.width, height: CGFloat(Int(size.height) - Int(size.height/2))))) else { return nil }
            return UIImage(cgImage: image, scale: scale, orientation: imageOrientation)
        }
        var leftHalf: UIImage? {
            guard let cgImage = cgImage, let image = cgImage.cropping(to: CGRect(origin: .zero, size: CGSize(width: size.width/2, height: size.height))) else { return nil }
            return UIImage(cgImage: image, scale: scale, orientation: imageOrientation)
        }
        var rightHalf: UIImage? {
            guard let cgImage = cgImage, let image = cgImage.cropping(to: CGRect(origin: CGPoint(x: CGFloat(Int(size.width)-Int((size.width/2))), y: 0), size: CGSize(width: CGFloat(Int(size.width)-Int((size.width/2))), height: size.height)))
                else { return nil }
            return UIImage(cgImage: image, scale: scale, orientation: imageOrientation)
        }
        var splitedInFourParts: [UIImage] {
            guard case let topHalf = topHalf,
                  case let bottomHalf = bottomHalf,
                let topLeft = topHalf?.leftHalf,
                let topRight = topHalf?.rightHalf,
                let bottomLeft = bottomHalf?.leftHalf,
                let bottomRight = bottomHalf?.rightHalf else{ return [] }
            return [topLeft, topRight, bottomLeft, bottomRight]
        }
        var splitedInSixteenParts: [UIImage] {
            var array = splitedInFourParts.flatMap({$0.splitedInFourParts})
            // if you need it in reading order you need to swap some image positions
            swap(&array[2], &array[4])
            swap(&array[3], &array[5])
            swap(&array[10], &array[12])
            swap(&array[11], &array[13])
            return array
        }
    }
    

    Splitting the image by columns and rows:

    extension UIImage {
        func matrix(_ rows: Int, _ columns: Int) -> [UIImage] {
            let y = (size.height / CGFloat(rows)).rounded()
            let x = (size.width / CGFloat(columns)).rounded()
            var images: [UIImage] = []
            images.reserveCapacity(rows * columns)
            guard let cgImage = cgImage else { return [] }
            (0..

    let myPicture = UIImage(data: try! Data(contentsOf: URL(string:"https://i.stack.imgur.com/Xs4RX.jpg")!))!
    
    let images = myPicture.matrix(4, 4)
    
    images.count   // 16
    

提交回复
热议问题