I'm taking UIImages and sending them over iOS' Multipeer framework. Here's the steps I take to do so:
/// Save images to filesystem before sending over Multipeer
func save(image: UIImage) -> ImageFile? {
guard let imageDirectory = imageDirectory,
ensureDirectoryExists(imageDirectory),
let data = image.pngData()
else {
return nil
}
let id = UUID()
let filepath = (imageDirectory as NSString).appendingPathComponent(id.uuidString + ".png")
let fileURL = URL(fileURLWithPath: filepath)
do {
try data.write(to: fileURL, options: .atomic)
} catch {
print(error)
}
return ImageFile(id: id, url: fileURL)
}
/// Send the photos that were saved to the filesystem over Multipeer
func sendImages() {
for imageFile in imageFiles {
session.sendResource(at: imageFile.url, name: imageFile.id.uuidString, to: session.peers)
}
}
/// On receiving clients, load the image from the URL
func loadImage(at url: URL) -> UIImage? {
do {
let data = try Data(contentsOf: url)
return UIImage(data: data)
} catch {
print(error)
}
return nil
}
The problem is some (not all) images come out on the receiving clients rotated 90º counterclockwise.
Is there something I should be doing differently when loading images from the filesystem, or saving to the filesystem? I'm guessing it has something to do with EXIF data, or maybe images that have been manually rotated?