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

时间:2016-09-23 07:30:49

标签: ios swift uiimage core-graphics cgimage

目标:裁剪 var input = [{'from':'a','to':'b','option':'100'}, {'from':'a','to':'b','option':'200'}, {'from':'a','to':'b','option':'300'}, {'from':'c','to':'d','option':'400'}, { 'from': 'c', 'to': 'd', 'option': '500' }]; var tmp = {}; $.each(input, function (idx, obj) { var key = obj.from + obj.to tmp[key] = tmp[key] || { from: obj.from, to: obj.to}; tmp[key].option = tmp[key].option || []; tmp[key].option.push(obj.option); }); var output = []; for(var key in tmp) { output.push(tmp[key]); } (以UIImage属性2.0开头)

我执行以下代码:

scale

此代码有效,但结果let croppedCGImage = originalUIImage.cgImage!.cropping(to: cropRect) let croppedUIImage = UIImage(cgImage: croppedCGImage!) 的{​​{1}}属性不正确为1.0。

我在创建最终图片时尝试指定croppedUIImage

scale

这会产生正确的比例,但会将scale尺寸缩小一半。

我该怎么办?

(*注意:UIImage上的let croppedUIImage = UIImage(cgImage: croppedCGImage!, scale: 2.0, orientation: .up) 属性非常重要,因为我稍后使用受size属性影响的scale保存图像

编辑:

我得到以下工作。不幸的是,它比UIImagePNGRepresentation(_ image: UIImage)裁剪功能慢得多。

scale

4 个答案:

答案 0 :(得分: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!
    }
}

答案 1 :(得分:4)

使用此扩展程序: -

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

答案 2 :(得分:3)

我正在使用适用于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?

答案 3 :(得分: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];