ios - 在 CloudKit 中将图像存储为 CKAsset,图像倒置

标签 ios swift cloudkit ckasset

我正在开发一个应用程序,该应用程序使用 CloudKit 将图像文件存储和检索为 CKAsset 对象。通常,这很好用,我喜欢 CloudKit 的小学习曲线。不过,我会定期遇到这个特殊问题,即图像要么完全倒置,要么向左或向右 90º 存储。这是存储不当的图像示例:

Example of my MacBook curiously rotated 90º to the left

我用我的 iPhone 5s 纵向拍摄了这张照片(我没有将原始图像保存到我的设备,因为我只想将它保存在云端,否则我也会在这里发布原始图像。我因为它是坐在 table 上的 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")
                        }
                }
            }
        })
      }

这里的 game 只是我创建的一个名为 SavedGameNSObject,它只存储变量,特别是在这种情况下 referenceImage 作为 UIImage。我很确定我的检索过程不是问题,因为我注意到当我转到 CK Dashboard 并在图像存储后下载图像时,它也在那里旋转。为了以防万一,这是我用来检索图像的代码:

// 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 文件。我从这个 SO question 得到这个.它发布在讨论的下方,所以我在这里复制它。

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()

关于ios - 在 CloudKit 中将图像存储为 CKAsset,图像倒置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35973296/

相关文章:

ios - 是否需要将 scrollview.delegate 设置为 self?

ios - 绘图函数中是否允许计算

ios - 克隆 Photo.app?

core-data - 今日 iCloud + 核心数据(2015 年 7 月 10 日)

ios - 使用新的照片框架保存一大批照片?

ios - Xcode6 - ibtool 失败,退出代码为 255

swift - Firebase,IN 子句查询和数据列表

ios - 在协议(protocol)扩展中将 nil 传递给具有可选的一般约束参数的函数

AppDelegate 和 ContentView() 之间的 SwiftUI 通信

ios - 如何将 CKQueryNotification 连接到 CKRecord 或 CKSubsription?