//
//  ZZHImageExtension.swift
//  SwiftProject
//
//  Created by aaa on 2024/3/15.
//

import Foundation
import UIKit
import CoreImage
let EXImageContext = CIContext()
extension UIImage {
    
    func imageAtRect(rect: CGRect) -> UIImage? {
        // 确保CGRect在图片范围内
        guard let cgImage = self.cgImage else {
            guard let ciImage = self.ciImage else {
                return nil
            }
            // 将CIImage转换为CGImage
            let imageRef = ciImage.cropped(to: rect)
            //此处根据文档说明要求,必须转换坐标才能正常展示
            let final = EXImageContext.createCGImage(imageRef, from:imageRef.extent)
            return  UIImage(cgImage: final!)
        }
        print("cgimage.....>>>>>>>>>>>>>.")
        let imageRef = cgImage.cropping(to: rect)
        return imageRef == nil ? nil : UIImage(cgImage: imageRef!)
    }
    
//    func rotate( radians: CGFloat) -> UIImage? {
//        // 计算旋转角度对应的弧度
//        let radians = radians * CGFloat.pi / 180
//        // 创建一个2D转换
//        var transform = CGAffineTransform(rotationAngle: radians)
//        // 计算变换后的图片大小
//        let rotatedViewBox = UIView(frame: CGRect(origin: .zero, size: self.size))
//        rotatedViewBox.transform = transform
//        let rotatedSize = rotatedViewBox.frame.size
//        // 开始图形上下文
//        UIGraphicsBeginImageContextWithOptions(rotatedSize, false, 1.0)
//        defer { UIGraphicsEndImageContext() }
//        // 创建一个相同大小的视图，并应用变换
//        let rotatedView = UIView(frame: CGRect(origin: .zero, size: rotatedSize))
//        rotatedView.transform = transform
//        // 将image绘制到上下文
//        self.draw(in: rotatedView.frame)
//        // 从上下文获取新图片
//        return UIGraphicsGetImageFromCurrentImageContext()
//    }
//    
//    
    func rotate(radians: CGFloat) -> UIImage {
        let rotatedSize = CGRect(origin: .zero, size: size)
            .applying(CGAffineTransform(rotationAngle: CGFloat(radians)))
            .integral.size
        UIGraphicsBeginImageContext(rotatedSize)
        if let context = UIGraphicsGetCurrentContext() {
            let origin = CGPoint(x: rotatedSize.width / 2.0,
                                 y: rotatedSize.height / 2.0)
            context.translateBy(x: origin.x, y: origin.y)
            context.rotate(by: radians)
            draw(in: CGRect(x: -origin.y, y: -origin.x,
                            width: size.width, height: size.height))
            let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            return rotatedImage ?? self
        }
        
        return self
    }
    
    //返回正向图片
    func getUpImg(imgOrigOritentation:UIImage.Orientation)->UIImage {
        
        let dg = getRotateFrom(imgOrigOritentation)
        let rotatedImage = rotate(radians:dg)
        return rotatedImage
    }
    
    func getRotateFrom(_ imgOrigOritentation:UIImage.Orientation) ->CGFloat {
        print("..or:\(imgOrigOritentation.rawValue)")
        if imgOrigOritentation == .left {
            return -.pi/2
        } else if imgOrigOritentation == .right{
            return .pi/2
        }
        else {//对于down、up的暂时不用管，因为空间照片要求横着拍，所以这里就先随便写一个方向，待后面需要时再做调整
            return .pi
        }
    }
}

