我正在尝试将照片选择器应用到我的应用中。出于某种原因,我在两个函数中收到以下错误:“StorageReference”类型的值没有成员“data”,类型“StorageReference”的值没有成员“put”。这是我的代码。老实说,我不确定问题似乎是什么。任何投入将不胜感激。感谢。
import UIKit
import FirebaseStorage
import Photos
class AlbumViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var id: String = ""
let picker = UIImagePickerController()
@IBOutlet weak var image1: UIImageView!
let storage = Storage.storage()
override func viewDidLoad() {
super.viewDidLoad()
self.title = id
picker.delegate = self as! UIImagePickerControllerDelegate & UINavigationControllerDelegate
showPhoto()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//confirm
func confirm() {
let myAlert = UIAlertController(title: "Firebase Storage", message: "Image Uploaded", preferredStyle: UIAlertControllerStyle.alert)
myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(myAlert, animated: true, completion: nil)
}
//retrive pictures
@IBAction func accessAlbum(_ sender: UIBarButtonItem) {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
picker.modalPresentationStyle = .popover
present(picker, animated: true, completion: nil)
picker.popoverPresentationController?.barButtonItem = sender
}
//for image size
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print(info)
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage
{
selectedImageFromPicker = editedImage
}else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker
{
image1.image = selectedImage.resizedImageWithinRect(rectSize: CGSize(width: 300, height: 300))
image1.contentMode = .scaleAspectFit //3
}
dismiss(animated: true, completion: nil)
}
@IBAction func savePic(_ sender: Any) {
var data = Data()
data = UIImagePNGRepresentation(image1.image!)! as Data
// set upload path
let imageName = "Photos/" + id + ".png"
// ---- Upload the image to firebase storage
let storageRef = storage.reference(withPath: imageName)
let metaData = StorageMetadata()
metaData.contentType = "image/png"
storageRef.put(data, metadata: metaData){(metaData,error) in //ERROR HERE
if let error = error {
print(error.localizedDescription)
return
}else{
self.confirm()
}
}
}
func showPhoto() {
let imageName = "Photos/" + id + ".png"
// Create a storage reference path with image name
let storageRef = storage.reference(withPath: imageName)
storageRef.data(withMaxSize: 3 * 1024 * 1024) { (data, error) // ERROR HERE-> Void in
// Create a UIImage, add it to the array
if let imgData = data {
let image = UIImage(data: imgData)
DispatchQueue.main.async {
self.myImageView.image = image
}
//
} else {
print ("no image data")
}
}
}
}
extension UIImage {
/// Returns a image that fills in newSize
func resizedImage(newSize: CGSize) -> UIImage {
// Guard newSize is different
guard self.size != newSize else { return self }
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
// self.drawInRect(CGRect(0, 0, newSize.width, newSize.height))
self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
/// Returns a resized image that fits in rectSize, keeping it's aspect ratio
/// Note that the new image size is not rectSize, but within it.
func resizedImageWithinRect(rectSize: CGSize) -> UIImage {
let widthFactor = size.width / rectSize.width
let heightFactor = size.height / rectSize.height
var resizeFactor = widthFactor
if size.height > size.width {
resizeFactor = heightFactor
}
let newSize = CGSize(width: size.width/resizeFactor, height: size.height/resizeFactor)
// CGSize(size.width/resizeFactor, size.height/resizeFactor)
let resized = resizedImage(newSize: newSize)
return resized
}
}