【发布时间】:2017-02-01 23:41:28
【问题描述】:
我的部分 iOS 应用使用 Firebase 存储来存储和加载用户拍摄/上传的图像。 但是,每次我以纵向将图片上传到 Firebase 时,当我稍后从 Firebase 检索它时,它会以横向显示,因此显示不正确。
我读到 Firebase 存储元数据以确定上传图片的正确方向,但它似乎对我不起作用。
任何人都知道如何获取图像上传的方向,而无需在应用程序代码中手动旋转它?
谢谢!
编辑代码:
上传中:
let imageMetaData = FIRStorageMetadata()
imageMetaData.contentType = "image/png"
var imageData = Data()
imageData = UIImagePNGRepresentation(chosenImage)!
let currentUserProfilePictureRef = currentUserStorageRef.child("ProfilePicture")
currentUserProfilePictureRef.put(imageData, metadata: imageMetaData) { (metaData, error) in
if error == nil {
let downloadUrl = metaData?.downloadURL()?.absoluteString
self.currentUserRef.updateChildValues(["profilePictureUrl": downloadUrl!])
if (chosenImage.size.width.isLess(than: (chosenImage.size.height))) {
self.currentUserRef.updateChildValues(["profilePictureOrientation": "portrait"])
}
else {
self.currentUserRef.updateChildValues(["profilePictureOrientation": "landscape"])
}
}
}
检索:
self.currentUserStorageRef.child("ProfilePicture").data(withMaxSize: 20*1024*1024, completion: {(data, error) in
var profilePicture = UIImage(data:data!)
if(profilePicture?.size.width.isLess(than: (profilePicture?.size.height)!))! {
if (pictureOrientation == "landscape") {
profilePicture = profilePicture?.rotated(by: Measurement(value: -90.0, unit: .degrees))
}
} else {
if (pictureOrientation == "portrait") {
profilePicture = profilePicture?.rotated(by: Measurement(value: 90.0, unit: .degrees))
}
}
self.buttonProfilePicture.setImage(profilePicture, for: .normal)
self.buttonProfilePicture.imageView?.contentMode = .scaleAspectFill
})
【问题讨论】:
-
您可以添加用于存储和检索图像的代码吗?
-
在获取保存在 CloudKit 中的图像时,我随机发生了类似的事情。您是否使用
PNGRepresentation保存它们? -
是的,使用 PNGRepresentation!这是这里的问题吗?
-
编辑原始问题以添加代码。我目前正在手动检查图像是纵向还是横向上传,然后在必要时旋转图像,因为这不起作用。
-
一年前我问过同样的问题stackoverflow.com/q/35973296/5378116
标签: ios swift firebase firebase-storage