【发布时间】:2017-01-01 03:43:08
【问题描述】:
我正在尝试将图像从图像视图上传到 Firebase 存储。
用户将从相册或相机上传图像到图像视图!我想要的是将此图像从图像视图上传到 Firebase 存储。
我尝试按照此处的 Firebase 文档中的示例进行操作:
let localFile: NSURL = ImageView.image
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
// Upload file and metadata to the object 'images/mountains.jpg'
let uploadTask = storage.child("images/DeviceImage.jpg").putFile(localFile, metadata: metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.observeStatus(.Pause) { snapshot in
// Upload paused
}
uploadTask.observeStatus(.Resume) { snapshot in
// Upload resumed, also fires when the upload starts
}
uploadTask.observeStatus(.Progress) { snapshot in
// Upload reported progress
if let progress = snapshot.progress {
let percentComplete = 100.0 * Double(progress.completedUnitCount) / Double(progress.totalUnitCount)
}
}
uploadTask.observeStatus(.Success) { snapshot in
// Upload completed successfully
}
// Errors only occur in the "Failure" case
uploadTask.observeStatus(.Failure) { snapshot in
guard let storageError = snapshot.error else { return }
guard let errorCode = FIRStorageErrorCode(rawValue: storageError.code) else { return }
switch errorCode {
case .ObjectNotFound:
// File doesn't exist
case .Unauthorized:
// User doesn't have permission to access file
case .Cancelled:
// User canceled the upload
...
case .Unknown:
// Unknown error occurred, inspect the server response
}
}
但我不知道如何将 UIImageView 转换为 NSURL。
【问题讨论】:
-
首先确保图像存在于你的磁盘上,然后从它的路径创建一个 NSURL。
标签: ios swift uiimage nsurl firebase-storage