Initial Create

基础框架实现
This commit is contained in:
2025-08-16 23:45:52 +08:00
parent de8ac20776
commit cb7294d722
14 changed files with 472 additions and 24 deletions

View File

@@ -0,0 +1,138 @@
import UIKit
extension UIImage {
func decodeImage(num: Int) -> UIImage {
guard let decodedImage = decodeImage2(num: num) else {
print("解密失败,返回原图")
return self
}
//
// 2.
//let flippedImage = decodedImage.flipVertically()
// 3.
let mirroredImage = decodedImage.wrongFlipHorizontally().correctFlipHorizontally()
return mirroredImage
}
//
private func wrongFlipHorizontally() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext(),
let cgImage = self.cgImage else {
print("镜像失败:无法获取 CGImage")
return self
}
//
context.translateBy(x: self.size.width, y: 0)
context.scaleBy(x: -1.0, y: 1.0)
context.draw(cgImage, in: CGRect(origin: .zero, size: self.size))
return UIGraphicsGetImageFromCurrentImageContext() ?? self
}
//
private func correctFlipHorizontally() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext(),
let cgImage = self.cgImage else {
print("镜像失败:无法获取 CGImage")
return self
}
//
context.translateBy(x: self.size.width, y: self.size.height)
context.scaleBy(x: -1.0, y: -1.0) // x
context.draw(cgImage, in: CGRect(origin: .zero, size: self.size))
return UIGraphicsGetImageFromCurrentImageContext() ?? self
}
//
private func decodeImage2(num: Int) -> UIImage? {
guard num > 0 else {
print("解密跳过num 参数为 0直接返回原图")
return self
}
let width = Int(self.size.width * self.scale)
let height = Int(self.size.height * self.scale)
let totalHeight = height //
let totalWidth = width
UIGraphicsBeginImageContextWithOptions(CGSize(width: totalWidth, height: totalHeight), false, self.scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext(),
let cgImage = self.cgImage else {
print("解密失败:无法创建图形上下文或获取 CGImage")
return nil
}
let segmentHeight = totalHeight / num //
let remainder = totalHeight % num //
print("图片总高度: \(totalHeight),每段高度: \(segmentHeight),余数: \(remainder)")
for i in 0..<num {
var currentSegmentHeight = segmentHeight
let ySrc = segmentHeight * i //
var yDst = segmentHeight * i //
if i == 0 {
currentSegmentHeight += remainder //
}
let srcRect = CGRect(
x: 0,
y: CGFloat(ySrc),
width: CGFloat(totalWidth),
height: CGFloat(currentSegmentHeight)
)
let dstRect = CGRect(
x: 0,
y: CGFloat(yDst),
width: CGFloat(totalWidth),
height: CGFloat(currentSegmentHeight)
)
print("\(i): 源矩形: \(srcRect),目标矩形: \(dstRect)")
if let croppedImage = cgImage.cropping(to: srcRect) {
context.draw(croppedImage, in: dstRect)
} else {
print("\(i): 裁剪失败")
}
}
return UIGraphicsGetImageFromCurrentImageContext()
}
//
private func flipVertically() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext(),
let cgImage = self.cgImage else {
print("翻转失败:无法获取 CGImage")
return self
}
//
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0)
//
context.draw(cgImage, in: CGRect(origin: .zero, size: self.size))
return UIGraphicsGetImageFromCurrentImageContext() ?? self
}
}