Cropping center square of UIImage

后端 未结 18 666
日久生厌
日久生厌 2020-12-07 23:09

So here\'s where I\'ve made it so far. I am using a UIImage captured from the camera and can crop the center square when in landscape. For some reason this doesn\'t transl

18条回答
  •  我在风中等你
    2020-12-07 23:31

    There is the swift version for the checked solution :

    private func imageByCroppingImage(image : UIImage, size : CGSize) -> UIImage{
            var refWidth : CGFloat = CGFloat(CGImageGetWidth(image.CGImage))
            var refHeight : CGFloat = CGFloat(CGImageGetHeight(image.CGImage))
    
            var x = (refWidth - size.width) / 2
            var y = (refHeight - size.height) / 2
    
            let cropRect = CGRectMake(x, y, size.height, size.width)
            let imageRef = CGImageCreateWithImageInRect(image.CGImage, cropRect)
    
            let cropped : UIImage = UIImage(CGImage: imageRef, scale: 0, orientation: image.imageOrientation)!
    
    
            return cropped
        }
    

提交回复
热议问题