解决照片拍摄之后附加方向oritentation进入image中

This commit is contained in:
bluesea 2024-03-25 18:43:41 +08:00
parent e46743321d
commit c517a6ede6

View File

@ -33,7 +33,6 @@ class CCSpatialShootController: BaseController {
var isRecording = false
var imageCGImagePropertyOrientation:CGImagePropertyOrientation = CGImagePropertyOrientation.left//cgimage
var imageOrientation:UIImage.Orientation = UIImage.Orientation.up
var deviceOrientation = 1// 1,2
let operationQueue:OperationQueue = OperationQueue()
@ -890,6 +889,12 @@ class CCSpatialShootController: BaseController {
hidenHorizontalScreenTips()
print("横屏");
}
if x >= 0{//
imageCGImagePropertyOrientation = .right
}
else{//
imageCGImagePropertyOrientation = .left
}
}
}
@ -1114,28 +1119,6 @@ extension CCSpatialShootController: AVCaptureFileOutputRecordingDelegate {
}
}
func convertOrigation(orientation:UIImage.Orientation) -> CGImagePropertyOrientation{
switch orientation {
case UIImage.Orientation.left:
return CGImagePropertyOrientation.left
case UIImage.Orientation.right:
return CGImagePropertyOrientation.right
case UIImage.Orientation.up:
return CGImagePropertyOrientation.up
case UIImage.Orientation.down:
return CGImagePropertyOrientation.down
case UIImage.Orientation.leftMirrored:
return CGImagePropertyOrientation.leftMirrored
case UIImage.Orientation.rightMirrored:
return CGImagePropertyOrientation.rightMirrored
case UIImage.Orientation.upMirrored:
return CGImagePropertyOrientation.upMirrored
case UIImage.Orientation.downMirrored:
return CGImagePropertyOrientation.downMirrored
@unknown default:
return CGImagePropertyOrientation.up
}
}
//
func compositeSpatialPhoto(){
@ -1144,7 +1127,8 @@ extension CCSpatialShootController: AVCaptureFileOutputRecordingDelegate {
let url = URL.documentsDirectory.appending(path:"aaa12.HEIC")
let destination = CGImageDestinationCreateWithURL(url as CFURL, UTType.heic.identifier as CFString, 2, nil)!
imageCGImagePropertyOrientation = .left
var oo = imageCGImagePropertyOrientation.rawValue
let orientation_cf = CFNumberCreate(nil, CFNumberType.intType, &oo)
let properties1 = [
kCGImagePropertyGroups: [
kCGImagePropertyGroupIndex: 0,
@ -1152,7 +1136,11 @@ extension CCSpatialShootController: AVCaptureFileOutputRecordingDelegate {
kCGImagePropertyGroupImageIndexLeft: 0,
kCGImagePropertyGroupImageIndexRight: 1,
],
kCGImagePropertyTIFFOrientation:imageCGImagePropertyOrientation,
kCGImagePropertyTIFFDictionary:[
kCGImagePropertyOrientation:orientation_cf,
],
kCGImagePropertyOrientation:orientation_cf as Any,
kCGImagePropertyHEIFDictionary: [
kIIOMetadata_CameraExtrinsicsKey: [
kIIOCameraExtrinsics_CoordinateSystemID: 0,
@ -1169,7 +1157,6 @@ extension CCSpatialShootController: AVCaptureFileOutputRecordingDelegate {
]
]
] as [CFString : Any]
let properties2 = [
kCGImagePropertyGroups: [
kCGImagePropertyGroupIndex: 0,
@ -1177,7 +1164,11 @@ extension CCSpatialShootController: AVCaptureFileOutputRecordingDelegate {
kCGImagePropertyGroupImageIndexLeft: 0,
kCGImagePropertyGroupImageIndexRight: 1,
],
kCGImagePropertyTIFFOrientation:imageCGImagePropertyOrientation,
kCGImagePropertyTIFFDictionary:[
kCGImagePropertyOrientation:orientation_cf,
],
kCGImagePropertyOrientation:orientation_cf as Any,
kCGImagePropertyHEIFDictionary: [
kIIOMetadata_CameraExtrinsicsKey: [
kIIOCameraExtrinsics_CoordinateSystemID: 0,
@ -1199,13 +1190,11 @@ extension CCSpatialShootController: AVCaptureFileOutputRecordingDelegate {
let rightImg = img2//fixOrientation(img2)
// CGImageDestinationSetProperties(destination, [kCGImagePropertyOrientation: imageCGImagePropertyOrientation] as CFDictionary)
// CGImageDestinationSetProperties(destination,properties1 as CFDictionary)
// CGImageDestinationSetProperties(destination,properties2 as CFDictionary)
CGImageDestinationAddImage(destination, leftImg.cgImage!, properties1 as CFDictionary)
CGImageDestinationAddImage(destination, rightImg.cgImage!, properties2 as CFDictionary)
// CGImageDestinationAddImage(destination, leftImg.cgImage!, [kCGImagePropertyOrientation: imageCGImagePropertyOrientation] as CFDictionary)
//// CGImageDestinationAddImage(destination, rightImg.cgImage!, [kCGImagePropertyOrientation: imageCGImagePropertyOrientation] as CFDictionary)
let p_dic1:CFDictionary = properties1 as CFDictionary
CGImageDestinationAddImage(destination, leftImg.cgImage!,p_dic1)
let p_dic2:CFDictionary = properties2 as CFDictionary
CGImageDestinationAddImage(destination, rightImg.cgImage!, p_dic2)
let rr = CGImageDestinationFinalize(destination)
if rr == false {
@ -1214,85 +1203,16 @@ extension CCSpatialShootController: AVCaptureFileOutputRecordingDelegate {
let source = CGImageSourceCreateWithURL(url as CFURL, nil)!
guard let properties22 = CGImageSourceCopyPropertiesAtIndex(source, 1, nil) as? [CFString: Any] else {
return
}
print("ssss:\(properties22)")
// let source = CGImageSourceCreateWithURL(url as CFURL, nil)!
// guard let properties22 = CGImageSourceCopyPropertiesAtIndex(source, 1, nil) as? [CFString: Any] else {
// return
// }
// print("ssss:\(properties22)")
savePhoto(url)
}
//
func fixOrientation(_ image: UIImage) -> UIImage {
return image
// return UIImage(cgImage: image.cgImage!, scale: image.scale, orientation: imageOrientation)
// No-op if the orientation is already correct
guard image.imageOrientation != .up else { return image }
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform = CGAffineTransform.identity
switch image.imageOrientation {
case .down, .downMirrored:
transform = transform.translatedBy(x: image.size.width, y: image.size.height)
transform = transform.rotated(by: .pi)
case .left, .leftMirrored:
transform = transform.translatedBy(x: image.size.width, y: 0)
transform = transform.rotated(by: .pi / 2)
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0, y: image.size.height)
transform = transform.rotated(by: -.pi / 2)
default:
break
}
switch image.imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: image.size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: image.size.height, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
default:
break
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
guard let cgImage = image.cgImage,
let colorSpace = cgImage.colorSpace,
let context = CGContext(data: nil,
width: Int(image.size.width),
height: Int(image.size.height),
bitsPerComponent: cgImage.bitsPerComponent,
bytesPerRow: 0,
space: colorSpace,
bitmapInfo: cgImage.bitmapInfo.rawValue)
else {
return image
}
context.concatenate(transform)
switch image.imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
// Grr...
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width))
default:
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
}
// And now we just create a new UIImage from the drawing context
guard let cgImageFixed = context.makeImage() else { return image }
let fixedImage = UIImage(cgImage: cgImageFixed)
return fixedImage
}