【问题标题】:Storing images in CloudKit as CKAsset, image upside-down在 CloudKit 中将图像存储为 CKAsset,图像倒置
【发布时间】:2016-03-13 17:22:19
【问题描述】:

我正在开发一个使用 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")
                        }
                }
            }
        })
      }

这里的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 问题,还是我的代码有问题。任何帮助将不胜感激。提前致谢!

【问题讨论】:

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

标签: ios swift cloudkit ckasset


【解决方案1】:

此代码适用于我的 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2017-05-17
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多