Robin Stern
Robin Stern

Reputation: 11

Crop UIImage without losing quality in swift

want to cut a UIImage into equal pieces. But when I cut it every piece has less quality than the original image. I used this function to cut it.

func cropImage2(image: UIImage, rect: CGRect, scale: CGFloat) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(CGSize(width: rect.size.width / scale, height: rect.size.height / scale), true, 0.0)
    image.draw(at: CGPoint(x: -rect.origin.x / scale, y: -rect.origin.y / scale))
    let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return croppedImage
}

Can anyone help or does someone have a better idea to cut an image into pieces in swift code without losing its original quality?

Upvotes: 0

Views: 255

Answers (1)

 Engineer
 Engineer

Reputation: 371

Instead of scaling image, it will loose it's quality. It's better to crop image into different parts.

Below solution, will help you to find better way to cut images into different parts, without loosing it's original quality.

Extension :

extension UIImage {
    var topHalf: UIImage? {
        guard 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: 1, orientation: imageOrientation)
    }
    var bottomHalf: UIImage? {
        guard let image = cgImage?
            .cropping(to: CGRect(origin: CGPoint(x: 0,
                                                 y: size.height - (size.height/2).rounded()),
                                 size: CGSize(width: size.width,
                                              height: size.height -
                                                (size.height/2).rounded())))
            else { return nil }
        return UIImage(cgImage: image)
    }
    var leftHalf: UIImage? {
        guard let image = cgImage?
            .cropping(to: CGRect(origin: .zero,
                                 size: CGSize(width: size.width/2,
                                              height: size.height)))
            else { return nil }
        return UIImage(cgImage: image)
    }
    var rightHalf: UIImage? {
        guard let image = cgImage?
            .cropping(to: CGRect(origin: CGPoint(x: size.width - (size.width/2).rounded(), y: 0),
                                 size: CGSize(width: size.width - (size.width/2).rounded(),
                                              height: size.height)))
            else { return nil }
        return UIImage(cgImage: image)
    }
}

ViewController :

class ViewController: UIViewController {


    @IBOutlet weak var topLeft: UIImageView!
    @IBOutlet weak var topRight: UIImageView!
    @IBOutlet weak var bottomLeft: UIImageView!
    @IBOutlet weak var bottomRight: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let image = UIImage(named: "cross") {
            let topHalf = image.topHalf
            let bottomHalf = image.bottomHalf

            let top_Left = topHalf?.leftHalf
            let top_Right = topHalf?.rightHalf
            let bottom_Left = bottomHalf?.leftHalf
            let bottom_Right = bottomHalf?.rightHalf

            //Assigning cropped image to different UIImageView's
            self.topLeft.image = top_Left
            self.topRight.image = top_Right
            self.bottomLeft.image = bottom_Left
            self.bottomRight.image = bottom_Right
        }
        else{
            print("No Image Found!")
        }   
    }
}

Hope this will resolve your issue.

Happy Coding :-)

Upvotes: 3

Related Questions