139 lines
5.0 KiB
Swift
139 lines
5.0 KiB
Swift
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
|
||
}
|
||
}
|