如何裁剪UIImage而不丢失其比例属性?

16

目标:剪裁一个以2.0缩放属性开始的UIImage

我执行以下代码:

let croppedCGImage = originalUIImage.cgImage!.cropping(to: cropRect)
let croppedUIImage = UIImage(cgImage: croppedCGImage!)

这段代码可以运行,但是结果croppedUIImagescale属性为错误的1.0。


我尝试在创建最终图像时指定scale

let croppedUIImage = UIImage(cgImage: croppedCGImage!, scale: 2.0, orientation: .up)

这会产生正确的比例,但会错误地将size维度减半。


我该怎么做呢?

(*注:UIImage上的scale属性很重要,因为我稍后使用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
    }
}
4个回答

7

试试这个:

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!
    }
}

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
    }
}

你觉得这段代码应该如何从矩形的位置和大小中裁剪图像??它对我不起作用。 - Maryam Fekri

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?

屏幕密度

// 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))

此处还包括: 图像特效等相关内容。
// 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?

1
-(UIImage *)getNeedImageFrom:(UIImage*)image cropRect:(CGRect)rect
{

    CGImageRef subImage = CGImageCreateWithImageInRect(image.CGImage, rect);
    UIImage *croppedImage = [UIImage imageWithCGImage:subImage];
    CGImageRelease(subImage);
    return croppedImage;
}

调用

    UIImage *imageSample=image;
    CGRect rectMake1=CGRectMake(0, 0,imageSample.size.width*1/4, imageSample.size.height);
    UIImage *img1=[[JRGlobal sharedInstance] getNeedImageFrom:imageSample cropRect:rectMake1];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接