如何裁剪UIImage而不失去它的规模属性?

bea*_*ain 15 core-graphics uiimage cgimage ios swift

目标:裁剪a UIImage(scale以2.0 的属性开头)

我执行以下代码:

let croppedCGImage = originalUIImage.cgImage!.cropping(to: cropRect)
let croppedUIImage = UIImage(cgImage: croppedCGImage!)
Run Code Online (Sandbox Code Playgroud)

此代码有效,但结果croppedUIImagescale属性不正确为1.0.


我已经尝试过指定scale创建最终图像的时间:

let croppedUIImage = UIImage(cgImage: croppedCGImage!, scale: 2.0, orientation: .up)
Run Code Online (Sandbox Code Playgroud)

这会产生正确的比例,但它会将size尺寸缩小一半.


我该怎么办?

(*注意:scaleUIImage上的属性很重要,因为我稍后会保存UIImagePNGRepresentation(_ image: UIImage)scale属性影响的图像)



编辑:

我得到以下工作.不幸的是,它比CGImage裁剪功能慢得多.

extension UIImage {
    func cropping(to rect: CGRect) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(rect.size, false, self.scale)

        self.draw(in: CGRect(x: -rect.origin.x, y: -rect.origin.y, width: self.size.width, height: self.size.height))

        let croppedImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return croppedImage
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

试试这个:

extension UIImage {
    func imageByCropToRect(rect:CGRect, scale:Bool) -> UIImage {

        var rect = rect
        var scaleFactor: CGFloat = 1.0
        if scale  {
            scaleFactor = self.scale
            rect.origin.x *= scaleFactor
            rect.origin.y *= scaleFactor
            rect.size.width *= scaleFactor
            rect.size.height *= scaleFactor
        }

        var image: UIImage? = nil;
        if rect.size.width > 0 && rect.size.height > 0 {
            let imageRef = self.cgImage!.cropping(to: rect)
            image = UIImage(cgImage: imageRef!, scale: scaleFactor, orientation: self.imageOrientation)
        }

        return image!
    }
}
Run Code Online (Sandbox Code Playgroud)


Rah*_*ani 5

使用此扩展:-

extension UIImage {

    func cropping(to quality: CGInterpolationQuality, rect: CGRect) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(rect.size, false, self.scale)

        let context = UIGraphicsGetCurrentContext()! as CGContext
        context.interpolationQuality = quality

        let drawRect : CGRect = CGRect(x: -rect.origin.x, y: -rect.origin.y, width: self.size.width, height: self.size.height)

        context.clip(to: CGRect(x:0, y:0, width: rect.size.width, height: rect.size.height))

        self.draw(in: drawRect)

        let croppedImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return croppedImage
    }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*eek 5

我正在使用适用于 iOS 和 tvOS的ImageHelper Pod,它运行良好,也可能满足您的需求。

它带来了很多UIImage扩展,例如:

裁剪和调整大小

// Crops an image to a new rect
func crop(bounds: CGRect) -> UIImage?

// Crops an image to a centered square
func cropToSquare() -> UIImage? {

// Resizes an image
func resize(size:CGSize, contentMode: UIImageContentMode = .ScaleToFill) -> UIImage?
Run Code Online (Sandbox Code Playgroud)

屏幕密度

// To create an image that is Retina aware, use the screen scale as a multiplier for your size. You should also use this technique for padding or borders.
let width = 140 * UIScreen.mainScreen().scale
let height = 140 * UIScreen.mainScreen().scale
let image = UIImage(named: "myImage")?.resize(CGSize(width: width, height: height))
Run Code Online (Sandbox Code Playgroud)

还有类似的东西:图像效果

// Applies a light blur effect to the image
func applyLightEffect() -> UIImage?
// Applies a extra light blur effect to the image
func applyExtraLightEffect() -> UIImage?
// Applies a dark blur effect to the image
func applyDarkEffect() -> UIImage?
// Applies a color tint to an image
func applyTintEffect(tintColor: UIColor) -> UIImage?
// Applies a blur to an image based on the specified radius, tint color saturation and mask image
func applyBlur(blurRadius:CGFloat, tintColor:UIColor?, saturationDeltaFactor:CGFloat, maskImage:UIImage? = nil) -> UIImage?
Run Code Online (Sandbox Code Playgroud)