私は iOS 開発が初めてで、Firebase Storage サービスを使用して画像を保存しようとしています。アプリに次のメソッドがありますが、UIImage への NSURL 参照を見つけることができません。次のメソッドを使用して、ライブラリから画像を取得します。
@IBAction func getImage(sender: UIButton) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(image, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let theInfo: NSDictionary = info as NSDictionary
let img: UIImage = theInfo.objectForKey(UIImagePickerControllerOriginalImage) as! UIImage
imageView.image = img
let localFile = info[UIImagePickerControllerReferenceURL] as! NSURL
uploadToFireBase(localFile)
self.dismissViewControllerAnimated(true, completion: nil)
}
そして、この方法を使用して、Firebaseにアップロードしようとします
func uploadToFireBase(localFile: NSURL) {
let storageRef = FIRStorage.storage().referenceForURL("gs://logintest-90287.appspot.com/Pictures")
let uploadTask = storageRef.putFile(localFile, metadata: nil) { metadata, error in
if (error != nil) {
// Uh-oh, an error occurred!
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
let downloadURL = metadata!.downloadURL
}
}
}
ただし、XCode は「本文ファイルに到達できません: /asset.JPG」と言い続けます。次の方法を使用して NSURL を取得しようとしましたが、これも機能しません。
let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.URLByAppendingPathComponent(imageName!)
Firebase に画像をアップロードする方法を教えてください。