上传/下载多个图像的正确方法? [英] uploading/downloading multiple images the right way?

查看:175
本文介绍了上传/下载多个图像的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 Nuke (用于下载和缓存图片的框架)上传或下载图片,然后 Firebase 上传图片作为后端


$ b

单个文件很容易处理没有任何问题
但是对于多个我不知道该怎么做正确
我有一个问题,它在哪里不要做同步工作
有时在第一张图片之前下载第二张图片



我将展示下载和上传多张图片的方式



下载我把代码放在for循环中

  func downloadImages :(结果:[ImageSource]) - > Void){
var images = [ImageSource]()
for i in 0 ... imageURLs.count-1 {

请让request = ImageRequest(URL:NSURL(string:imageURLs [i])!)

Nuke.taskWith(request){response $ is
response.isSuccess {
let image = ImageSource(image :response.image!)
images.append(image)
if i == self.imageURLs.count-1 {
completion(result:images)
}


$ b} .resume()


}

}
形式图像选择器并返回它作为NSData数组
然后执行此操作代码

  func uploadImages(完成:(结果:[字符串]) - > Void){
let storageRef = storage.referenceForURL(gs://project-xxxxxxxxx.appspot.com/Uploads/\(ref.childByAutoId()))
var imageUrl = [String] ()
var imgNum = 0



for i in 0 ... imageData.count-1 {
let imagesRef = storageRef.child( \(FIRAuth.auth()?currentUser?.uid)\(imgNum))
imgNum + = 1

let uploadTask = imagesRef.putData(imageData [i],元数据:无)元数据,在
错误如果(错误!=零){
打印(错误)
imageUrl = [字符串]()
完成(结果: imageUrl)
} else {

print(上传)
//元数据包含文件元数据,如大小,内容类型和下载URL。
let downloadURL = metadata!.downloadURL()?absoluteString
print(downloadURL)
imageUrl.append(downloadURL!)
if == imageUrl.count-1 { /结束循环
print(completionUpload)
completion(result:imageUrl)



}}

这是做这个任务的好方法吗?

我做每个图像的下载顺序吗?

请给我任何可能的帮助,例如代码,链接等等。
$ b $我们强烈建议您一起使用Firebase存储和Firebase实时数据库来完成下载列表:

共享:

  // Firebase服务
var数据库:FIRDatabase!
var storage:FIRStorage!
...
//初始化数据库,Auth,存储
database = FIRDatabase.database()
storage = FIRStorage.storage()
  let fileData = NSData 



()//获取数据...
let storageRef = storage.reference()。child(myFiles / myFile)
storageRef.putData(fileData).observeStatus(.Success){(snapshot)在
//当图片成功上传时,我们得到它的下载URL
让downloadURL = snapshot.metadata?.downloadURL()?。absoluteString
//将下载URL写入Realtime Database
let dbRef = database.reference()。child(myFiles / myFile)
dbRef.setValue(downloadURL)
}




$ b $ $ $ $ $ $ $ dbRef = database.reference() .child(myFiles)
dbRef.observeEventType(.ChildAdded,withBlock:{(snapshot)in
//从快照获取下载URL $ b $让downloadURL = snapshot.value()as!字符串

//现在使用N uke(或其他第三方库)
let request = ImageRequest(URL:NSURL(string:downloadURL)!)
Nuke.taskWith(request){
//
}

//或者,您可以使用Storage内置函数:
//从URL
创建存储引用let storageRef = storage.referenceFromURL( (下载URL)
//下载数据,假设最大大小为1MB(可以根据需要更改)
storageRef.dataWithMaxSize(1 * 1024 * 1024){(data,error) - >无效
//做一些数据...
})
})

有关详情,请参阅从零开始到应用:使用Firebase进行开发,它的相关的源代码,这是一个如何做到这一点的实际例子。


i'm trying to upload or download images using Nuke(framework for downloading and Caching images) And Firebase to upload images as the backend

for single file it's easy to deal with without any problem but for multiple ones i don't really know what to do right i'm having an issues where it don't do it job synchronously it downloads second image before the first one sometimes

i'll show my way of downloading and uploading multiple images

For download i put the code in for-loop

    func downloadImages(completion: (result: [ImageSource]) -> Void){
    var images = [ImageSource]()
    for i in 0...imageURLs.count-1{

        let request = ImageRequest(URL: NSURL(string:imageURLs[i])!)

        Nuke.taskWith(request) { response in
            if response.isSuccess{
        let image = ImageSource(image: response.image!)
                images.append(image)
                if i == self.imageURLs.count-1 {
                    completion(result: images)
                }
            }


        }.resume()


    }

}

And for uploading where the user chooses the images form image picker and return it as NSData array And then perform this code

    func uploadImages(completion: (result: [String]) -> Void){
    let storageRef = storage.referenceForURL("gs://project-xxxxxxxxx.appspot.com/Uploads/\(ref.childByAutoId())")
    var imageUrl = [String]()
    var imgNum = 0



    for i in 0...imageData.count-1 {
        let imagesRef = storageRef.child("\(FIRAuth.auth()?.currentUser?.uid) \(imgNum)")
        imgNum+=1

        let uploadTask =  imagesRef.putData(imageData[i], metadata: nil) { metadata, error in
            if (error != nil) {
                print("error")
                imageUrl = [String]()
                completion(result: imageUrl)
            } else {

                print("uploading")
                // Metadata contains file metadata such as size, content-type, and download URL.
                let downloadURL = metadata!.downloadURL()?.absoluteString
                print(downloadURL)
                imageUrl.append(downloadURL!)
                if i == imageUrl.count-1{ //end of the loop
                    print("completionUpload")
                    completion(result: imageUrl)

                }
            }
        }}

this is good way to do this task ?

what should i do to make each image downloads in order ?

please give me anything that may help example code , link etc ..

Thanks in advance

解决方案

We highly recommend using Firebase Storage and the Firebase Realtime Database together to accomplish lists of downloads:

Shared:

// Firebase services
var database: FIRDatabase!
var storage: FIRStorage!
...
// Initialize Database, Auth, Storage
database = FIRDatabase.database()
storage = FIRStorage.storage()

Upload:

let fileData = NSData() // get data...
let storageRef = storage.reference().child("myFiles/myFile")
storageRef.putData(fileData).observeStatus(.Success) { (snapshot) in
  // When the image has successfully uploaded, we get it's download URL
  let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
  // Write the download URL to the Realtime Database
  let dbRef = database.reference().child("myFiles/myFile")
  dbRef.setValue(downloadURL)
}

Download:

let dbRef = database.reference().child("myFiles")
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
  // Get download URL from snapshot
  let downloadURL = snapshot.value() as! String

  // Now use Nuke (or another third party lib)
  let request = ImageRequest(URL: NSURL(string:downloadURL)!)
  Nuke.taskWith(request) { response in
    // Do something with response
  }

  // Alternatively, you can use the Storage built-ins:
  // Create a storage reference from the URL
  let storageRef = storage.referenceFromURL(downloadURL)
  // Download the data, assuming a max size of 1MB (you can change this as necessary)
  storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
    // Do something with data...
  })
})

For more information, see Zero to App: Develop with Firebase, and it's associated source code, for a practical example of how to do this.

这篇关于上传/下载多个图像的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆