美文网首页
Swift + OC 裁剪图片相关代码

Swift + OC 裁剪图片相关代码

作者: 刘明洋 | 来源:发表于2020-12-26 16:32 被阅读0次

OC

 + (UIBezierPath *)creatBezierPath{
    /*
        将imageview的路径转换为图片的路径
        这样可以使图片在切割时不用缩放,防止图片失真
        */
       UIBezierPath *path = [UIBezierPath bezierPath];

     
    
       [path moveToPoint:CGPointMake(500, 100)];
       [path addLineToPoint:CGPointMake(700, 200)];
       [path addLineToPoint:CGPointMake(800, 400)];
       [path addLineToPoint:CGPointMake(400, 150)];
       [path closePath];
    
    return  path;
}


/// 根据path切割图片
/// - Parameters:
///   - path: 贝斯曲线
///   - image: 原来图片
///   - rect: 绘制后区间大小
/// - Returns: 返回裁剪好的图片
+ (UIImage*)clipWithPath:(UIBezierPath*)path image:(UIImage *)imageTemp rect:(CGRect)rect{

    @autoreleasepool {
        UIImage * image = [imageTemp copy];
  
        //开始绘制图片
        UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);

        CGContextRef contextRef = UIGraphicsGetCurrentContext();

        UIBezierPath * clipPath = path;
        CGContextAddPath(contextRef, clipPath.CGPath);
        CGContextClosePath(contextRef);
        CGContextClip(contextRef);

        //坐标系转换
        CGContextTranslateCTM(contextRef, 0, image.size.height);
        CGContextScaleCTM(contextRef, image.scale, -image.scale);
        CGRect drawRect = CGRectMake(0, 0, image.size.width, image.size.height);
        CGContextDrawImage(contextRef, drawRect, [image CGImage]);
        UIImage *destImg = UIGraphicsGetImageFromCurrentImageContext();

        //结束绘画
        UIGraphicsEndImageContext();
 
        //裁剪出来矩形
        UIImage * cImage = [TestManager yp_imagecutWithOriginalImage:destImg withCutRect:rect];
         
        return cImage;
    }
}





/// 通过位图(Bitmap)裁剪
/// - Parameters:
///   - image: 原图
///   - rect: 裁剪区域
/// - Returns: 返回裁剪后的图片
+ (UIImage *)yp_imagecutWithOriginalImage:(UIImage *)originalImage withCutRect:(CGRect)rect {
    CGImageRef subImageRef = CGImageCreateWithImageInRect(originalImage.CGImage, rect);
    CGRect smallRect = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    // 开启图形上下文
    UIGraphicsBeginImageContext(smallRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallRect, subImageRef);
    UIImage * image = [UIImage imageWithCGImage:subImageRef];
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    
    CGImageRelease(subImageRef);
    
    return image;
}

Swift

 class TMOCutImageManager: NSObject {
    
    static let sharedInstance = TMOCutImageManager()
    
     
    /// 根据坐标点,生产贝斯曲线
    /// - Parameter array: 所有CGPoint点数组
    /// - Returns: 返回path
    func creatBezierPath(array:[CGPoint])->UIBezierPath{
        let path:UIBezierPath = UIBezierPath()
        
        if array.count > 0{
            for (index, point) in array.enumerated() {
                if index == 0 {
                    path.move(to: point)
                }else{
                    path.addLine(to: point)
                }
            }
            path.close()
        } 
        return  path
    }
    
    /// 根据path切割图片
    /// - Parameters:
    ///   - path: 贝斯曲线
    ///   - image: 原来图片
    ///   - rect: 绘制后区间大小
    /// - Returns: 返回裁剪好的图片
    func clipWithPath(path:UIBezierPath,image:UIImage)->UIImage?{
        
        let newImage = image
        
        //开始绘制图片
        UIGraphicsBeginImageContextWithOptions(image.size, false, 1)
        
        if let  contextRef:CGContext = UIGraphicsGetCurrentContext(),
           let cgImage = newImage.cgImage{
            
            let clipPath:UIBezierPath = path
            
            contextRef.addPath(clipPath.cgPath)
            contextRef.closePath()
            contextRef.clip()
            
            //坐标系转换
            contextRef.translateBy(x: 0, y: newImage.size.height)
            contextRef.scaleBy(x: 1, y: -1)
            
            let drawRect = CGRect(x: 0, y: 0, width: newImage.size.width, height: newImage.size.height)
            contextRef.draw(cgImage, in: drawRect)
            
            let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()//结束绘画
            
            return croppedImage
        }
        return nil
    }
    
    
    
    /// 通过 CGImage 或 CIImage 裁剪
    /// - Parameters:
    ///   - image: 原图
    ///   - rect: 裁剪区域
    /// - Returns: 返回裁剪后的图片
    func imagecutWithOriginalImage(image:UIImage,rect:CGRect)->UIImage?{
        if let cgImage = image.cgImage,
           let croppedCgImage = cgImage.cropping(to: rect) {
            return UIImage(cgImage: croppedCgImage)
        } else if let ciImage = image.ciImage {
            let croppedCiImage = ciImage.cropped(to: rect)
            return UIImage(ciImage: croppedCiImage)
        }
        return nil
    }
    
    
    /// 通过位图(Bitmap)裁剪
    /// - Parameters:
    ///   - image: 原图
    ///   - rect: 裁剪区域
    /// - Returns: 返回裁剪后的图片
    func cropImage(_ image: UIImage, withRect rect: CGRect) -> UIImage? {
        UIGraphicsBeginImageContext(rect.size)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        context.translateBy(x: -rect.minX, y: -rect.minY)
        image.draw(at: .zero)
        let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return croppedImage
    }
}

相关文章

网友评论

      本文标题:Swift + OC 裁剪图片相关代码

      本文链接:https://www.haomeiwen.com/subject/anegnktx.html