Files
JetsonMediaIOS/Jetson Media/ui/UIImage.swift
Spasol cb7294d722 Initial Create
基础框架实现
2025-08-16 23:45:52 +08:00

139 lines
5.0 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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