Ios 将图像作为CKAsset存储在CloudKit中,图像倒置

Ios 将图像作为CKAsset存储在CloudKit中,图像倒置,ios,swift,cloudkit,ckasset,Ios,Swift,Cloudkit,Ckasset,我正在开发一个应用程序,它使用CloudKit将图像文件存储和检索为CKAsset对象。通常情况下,这非常有效,我喜欢CloudKit的小学习曲线。不过,我经常会遇到这样一个特殊的问题:图像要么完全倒置,要么向左或向右倾斜90度。以下是存储不正确的图像的示例: 我用我的iPhone 5s照了这张照片,是纵向的(我没有把原始图像保存到我的设备上,因为我只是想把它保存在云端,否则我也会把原始图像贴在这里。我想,因为它是一个坐在桌子旁的MacBook Pro,你可以计算出图片的原始方向)。不管怎样,

我正在开发一个应用程序,它使用CloudKit将图像文件存储和检索为
CKAsset
对象。通常情况下,这非常有效,我喜欢CloudKit的小学习曲线。不过,我经常会遇到这样一个特殊的问题:图像要么完全倒置,要么向左或向右倾斜90度。以下是存储不正确的图像的示例:

我用我的iPhone 5s照了这张照片,是纵向的(我没有把原始图像保存到我的设备上,因为我只是想把它保存在云端,否则我也会把原始图像贴在这里。我想,因为它是一个坐在桌子旁的MacBook Pro,你可以计算出图片的原始方向)。不管怎样,我有这个问题,不仅是从相机新的图像,而且从我的照片库中选择的图像

下面是我用来通过CloudKit存储图像的代码:

let newRecord:CKRecord = CKRecord(recordType: "ImageProject")
let imageData:NSData = UIImagePNGRepresentation(game.referenceImage)!
let path = self.documentsDirectoryPath.stringByAppendingPathComponent(self.imageDirectoryName)
imageURL = NSURL(fileURLWithPath: path)
imageData.writeToURL(imageURL, atomically: true)
UIImagePNGRepresentation(game.referenceImage)!.writeToFile(path, atomically: true)
let imageFile:CKAsset?  = CKAsset(fileURL: NSURL(fileURLWithPath: path))

newRecord.setValue(imageFile, forKey: "referenceImage")

    // Save the record into the public database
    if let database = self.publicDatabase {

      database.saveRecord(newRecord, completionHandler: { (record:CKRecord?, error:NSError?) in

            // Check if there was an error
            if error != nil {
                NSLog((error?.localizedDescription)!)
            }
            else {
                // There wasn't an error
                dispatch_async(dispatch_get_main_queue()) {

                        if let savedRecord = record {
                            print("Image Successfully Saved")
                        }
                }
            }
        })
      }
这里的
游戏
只是我创建的一个名为
SavedGame
NSObject
,它只存储变量,特别是在这种情况下,
referenceImage
作为
UIImage
。我很确定我的检索过程不是问题所在,因为我注意到当我在CK仪表板上下载存储后的图像时,图像也在那里旋转。以防万一,下面是我用来检索图像的代码:

// Initialize the database and start the query
..........
for record in returnedRecords {
    if let retrievedImage = record.objectForKey("referenceImage") as! CKAsset? {
         let newGame:SavedGame = SavedGame()                  
         if let data = NSData(contentsOfURL: retrievedImage.fileURL) {
              newGame.referenceImage =  UIImage(data: data)!
         }
     }
}

有谁能给我一些指导,告诉我哪里可能会出错?这个问题根本不一致,很奇怪。我估计有5-10%的情况会发生。我不知道这是CK问题,还是我的代码有问题。任何帮助都将不胜感激。提前谢谢

这段代码适用于png文件。我从这里得到这个。它被贴在讨论的下面,所以我在这里复制它

extension UIImage {
    func fixOrientation() -> UIImage {
        if self.imageOrientation == UIImageOrientation.up {
            return self
        }
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
        if let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() {
            UIGraphicsEndImageContext()
            return normalizedImage
        } else {
            return self
        }
    }
}
用法:

let cameraImage = //image captured from camera
let orientationFixedImage = cameraImage.fixOrientation()

似乎缺少方向信息,可能使用UIImageJPEGRepresentation(),但不使用UIImagePNGRepresentation。您能给我一个如何存储方向信息的示例吗?我知道如何进行JPEGRep,但不知道如何明确告诉资产方向。我不想做任何CGAffineTransformations,我想这是没有必要的。这取决于你如何获得图像。如果它来自iPhone摄像头或带有exif的jpeg,我相信你可以找到很多方法来获取它们的方向。如果图片来自某处,请从互联网下载。我建议不要将其更改为PNG,至少jpg本身可以保留其原始exif信息。感谢您的回复。我的应用程序只能从iPhone摄像头或手机照片库中获取图像。因此,如果我将代码更改为使用JPEG表示,这会自动指定imageData中的原始图像方向吗?是的,我想是的。如果使用JPEG表示,它还将在imageData中包含其exif信息。不客气:)