diff --git a/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift b/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift index 023f302..ab11b10 100644 --- a/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift +++ b/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialShootController.swift @@ -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,14 +1190,12 @@ 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 { print("ee..") @@ -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 - }