将图像以CKAsset的形式存储在CloudKit中,图像上下颠倒 [英] Storing images in CloudKit as CKAsset, image upside-down
问题描述
我正在开发一个使用CloudKit存储和检索图像文件作为CKAsset
对象的应用程序.通常,这很好用,我喜欢CloudKit的小学习曲线.但是,我会定期遇到一个特殊的问题,即图像完全颠倒存储,或者存储在左侧或右侧90º.这是一个图像存储不正确的示例:
I am working on developing an app that uses CloudKit to store and retrieve image files as CKAsset
objects. Normally, this works great and I love CloudKit's small learning curve. Periodically though, I have this peculiar issue where an image is being stored either totally upside-down, or 90º to the left or to the right. Here is an example of an image that was stored improperly:
我用iPhone 5s纵向拍摄了这张照片(我没有将原始图像保存到设备中,因为我只打算将其保存在云中,否则我也将原始图像发布到这里. (因为它是MacBook Pro坐在桌子上的缘故),您可以找出图片的原始预期方向).无论如何,我不仅从相机中获取新鲜的图像,而且从我的照片库中选择图像,也遇到了这个问题.
I took that picture with my iPhone 5s, in portrait orientation (I didn't save the original image to my device, as I only intended to save it in the cloud or else I'd post the original here too. I figured since it's a MacBook Pro sitting at a table you could figure out the original intended orientation of the picture). Anyways, I have this problem not only with images fresh from the camera, but also with an image chosen from my photo library.
这是我用来通过CloudKit存储图像的代码:
Here is the code I use to store the image via 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
只是我创建的称为SavedGame
的NSObject
,它仅存储变量,在这种情况下,专门将referenceImage
作为UIImage
.我很确定我的检索过程不是问题,因为我注意到当我进入CK仪表板并在存储图像后下载图像时,它也会在那里旋转.以防万一,这是我用来检索图像的代码:
Here the game
is just an NSObject
I have created called SavedGame
, that just stores variables, specifically for this case the referenceImage
as a UIImage
. I'm pretty sure that my retrieval process is not the issue, because I noticed when I go to the CK Dashboard and download the image after it's been stored, it's rotated there as well. Just in case, here is the code I use to retrieve the image:
// 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问题还是我的代码有问题.任何帮助将不胜感激.预先感谢!
Could anybody give me some sort of guidance as to where I might be making a mistake? This problem is not consistent at all, it's very strange. I would estimate it happens maybe 5-10% of the time. I don't know if its a CK issue, or a problem with my code. Any help would be greatly appreciated. Thanks in advance!
推荐答案
此代码对png文件适用于我.我是从 SO问题.它被张贴在讨论的后面,所以我在这里复制它.
This code works for me for png files. I got this from this SO question. It's posted way down the discussion so I'm reproducing it here.
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()
这篇关于将图像以CKAsset的形式存储在CloudKit中,图像上下颠倒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!