SwiftUI - 如何在上传到 Firebase 存储之前调整图像大小
SwiftUI - how can I resize images before uploading to Firebase Storage
我已成功将图像上传到存储,但我有一个问题,当我的图像尺寸很大时,渲染到 UI 会很慢。我想在上传到存储之前将我的图像调整为相同的默认大小。
func uploadImageToStorage(image: UIImage) {
if let imageData = image.jpegData(compressionQuality: 1) {
let storage = Storage.storage()
let storageRef = storage.reference()
let testRef = storageRef.child("avatar/\(user.id)/avatar.png")
testRef.putData(imageData, metadata: nil) {( _, error) in
if let error = error {
print("an error has occured - \(error.localizedDescription)")
} else {
print("image uploaded successfully")
}
}
} else {
print("Coldn't unwrap/case imgae to data")
}
}
1.定义一个函数来改变图像大小。
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
let rect = CGRect(origin: .zero, size: newSize)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
2。上传时调用该函数
func uploadImageToStorage(image: UIImage) {
if let imageData = resizeImage(image: image, targetSize: CGSize(width: 200, height: 200))?.pngData() {
let storage = Storage.storage()
let storageRef = storage.reference()
let testRef = storageRef.child("avatar/\(user.id)/avatar.png")
testRef.putData(imageData, metadata: nil) {( _, error) in
if let error = error {
print("an error has occured - \(error.localizedDescription)")
} else {
print("image uploaded successfully")
}
}
} else {
print("Coldn't unwrap/case imgae to data")
}
}
我已成功将图像上传到存储,但我有一个问题,当我的图像尺寸很大时,渲染到 UI 会很慢。我想在上传到存储之前将我的图像调整为相同的默认大小。
func uploadImageToStorage(image: UIImage) {
if let imageData = image.jpegData(compressionQuality: 1) {
let storage = Storage.storage()
let storageRef = storage.reference()
let testRef = storageRef.child("avatar/\(user.id)/avatar.png")
testRef.putData(imageData, metadata: nil) {( _, error) in
if let error = error {
print("an error has occured - \(error.localizedDescription)")
} else {
print("image uploaded successfully")
}
}
} else {
print("Coldn't unwrap/case imgae to data")
}
}
1.定义一个函数来改变图像大小。
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
let rect = CGRect(origin: .zero, size: newSize)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
2。上传时调用该函数
func uploadImageToStorage(image: UIImage) {
if let imageData = resizeImage(image: image, targetSize: CGSize(width: 200, height: 200))?.pngData() {
let storage = Storage.storage()
let storageRef = storage.reference()
let testRef = storageRef.child("avatar/\(user.id)/avatar.png")
testRef.putData(imageData, metadata: nil) {( _, error) in
if let error = error {
print("an error has occured - \(error.localizedDescription)")
} else {
print("image uploaded successfully")
}
}
} else {
print("Coldn't unwrap/case imgae to data")
}
}