我已经构建了应用程序,它在书籍的 UITableView 数据中显示。一切都很完美,我唯一需要的就是书的照片。要上传和检索照片,我使用 Firebase,但我不知道如何处理照片。
这是我实现的:
@IBAction func ButtonScatta(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func ButtonScegli(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [AnyHashable: Any]!) {
ImageView.image = image
self.dismiss(animated: true, completion: nil);
}
这是按钮“Vendi”的功能:
if let user = FIRAuth.auth()?.currentUser{
self.emailUser.text = user.email
let userID: String = user.uid
let x = libriArray.count
let y = String(x+1)
//let imageLibro: UIImage = self.ImageView.image!
let emailVenditore: String = self.emailUser.text!
let titoloLibro: String = self.TitoloText.text!
let codiceLibro: String = self.ISBNText.text!
let prezzoLibro: String = self.PrezzoText.text!
let edizioneLibro: String = self.EdizioneText.text!
let statoLibro: Bool = false
let Libro = ["titolo": titoloLibro, "codice": codiceLibro, "prezzo": prezzoLibro, "autore": edizioneLibro, "emailUser": emailVenditore, "userID": userID, "stato": statoLibro] as [String : Any]
let libriRef = ref.child(byAppendingPath: "Libri")
var libri = [y: Libro]
libriRef.childByAutoId().setValue(Libro)
self.dismiss(animated: true, completion: nil)
} else {
}
有人可以写下并解释如何上传和检索这些照片吗?
最佳答案
我很难准确地追踪您使用这些 var 名称所做的事情。但是,我假设您从 imagePickerController 获得了 UIImage。
您将需要 Firebase 存储(pod 'Firebase/Storage')并在相应文件中导入 FirebaseStorage。
以下是将 UIImage 上传到 Firebase 存储的方法:
func uploadPhoto(_ image: UIImage, completionBlock: @escaping () -> Void) {
let ref = FIRStorage.storage().reference().child("myCustomPath").child("myFileName.jpg") // you may want to use UUID().uuidString + ".jpg" instead of "myFileName.jpg" if you want to upload multiple files with unique names
let meta = FIRStorageMetadata()
meta.contentType = "image/jpg"
// 0.8 here is the compression quality percentage
ref.put(UIImageJPEGRepresentation(image, 0.8)!, metadata: meta, completion: { (imageMeta, error) in
if error != nil {
// handle the error
return
}
// most likely required data
let downloadURL = imageMeta?.downloadURL()?.absoluteString // needed to later download the image
let imagePath = imageMeta?.path // needed if you want to be able to delete the image later
// optional data
let timeStamp = imageMeta?.timeCreated
let size = imageMeta?.size
// ----- should save these data in your database at this point -----
completionBlock()
})
}
这是一个简单的函数,可以将 UIImage 上传到 Firebase Storage。请注意,您应该跟踪下载 URL 和您上传的每张图片的路径。您可以在上传后将它们保存在数据库中。
要下载您上传的图片,您可以这样做:
func retrieveImage(_ URL: String, completionBlock: @escaping (UIImage) -> Void) {
let ref = FIRStorage.storage().reference(forURL: URL)
// max download size limit is 10Mb in this case
ref.data(withMaxSize: 10 * 1024 * 1024, completion: { retrievedData, error in
if error != nil {
// handle the error
return
}
let image = UIImage(data: retrievedData!)!
completionBlock(image)
})
}
关于ios - 从 Firebase 上传和检索照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41313669/
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
默认情况下:回形针gem将所有附件存储在公共(public)目录中。出于安全原因,我不想将附件存储在公共(public)目录中,所以我将它们保存在应用程序根目录的uploads目录中:classPost我没有指定url选项,因为我不希望每个图像附件都有一个url。如果指定了url:那么拥有该url的任何人都可以访问该图像。这是不安全的。在user#show页面中:我想实际显示图像。如果我使用所有回形针默认设置,那么我可以这样做,因为图像将在公共(public)目录中并且图像将具有一个url:Someimage:看来,如果我将图像附件保存在公共(public)目录之外并且不指定url(同