diff --git a/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift b/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift index d94eccd3d..eac1a83e2 100644 --- a/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift +++ b/ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift @@ -374,13 +374,15 @@ nonisolated struct MediaUploadingPreprocessor { throw .failedStrippingLocationData } - guard let originalMetadata = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil), - (originalMetadata as NSDictionary).value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") != nil else { + let count = CGImageSourceGetCount(imageSource) + let hasLocation = (0.. URL { + let directory = FileManager.default.temporaryDirectory.appendingPathComponent("\(Self.self)-\(UUID().uuidString)") + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + let url = directory.appendingPathComponent("second_image_location_test_image.heic") + + let extent = CGRect(x: 0, y: 0, width: 64, height: 64) + let image = try #require(CIContext().createCGImage(CIImage(color: .gray).cropped(to: extent), from: extent)) + let destination = try #require(CGImageDestinationCreateWithURL(url as NSURL, UTType.heic.identifier as CFString, 2, nil)) + CGImageDestinationAddImage(destination, image, nil) + let location = [kCGImagePropertyGPSDictionary: [kCGImagePropertyGPSLatitude: 51.5, kCGImagePropertyGPSLongitude: 0.1]] + CGImageDestinationAddImage(destination, image, location as NSDictionary) + try #require(CGImageDestinationFinalize(destination)) + + let imageSource = try #require(CGImageSourceCreateWithURL(url as NSURL, nil)) + let firstMetadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as NSDictionary?) + let secondMetadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, 1, nil) as NSDictionary?) + try #require(firstMetadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") == nil) + try #require(secondMetadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") != nil) + + return url + } + /// Generates a HEIC laid out like an iPhone spatial photo in a unique temporary directory: a monoscopic primary image, then the right and /// left eyes, all in one stereo pair group. Every image has GPS metadata and an HDR gain map of its own size, and each eye has its camera. private func makeSpatialImageWithLocation() throws -> URL {