【问题标题】:Converting the UIImage view to NSURL to upload it to Firebase将 UIImage 视图转换为 NSURL 以将其上传到 Firebase
【发布时间】: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


【解决方案1】:

您需要将图像转换为磁盘上的文件并获取要上传的 url。

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let filePath = "\(paths[0])/MyImageName.jpg"

// Save image.
UIImageJPEGRepresentation(image, 0.90)?.writeToFile(filePath, atomically: true)

见:How do I save a UIImage to a file?

一旦将其写入磁盘,您就可以获得文件的 url,如下所示:

let localFile = NSURL(fileURLWithPath: filePath)

【讨论】:

  • UIImageView 上的 .image 属性包含对 UIImage 数据的引用。让 image = myImageView.image
  • 我会尽力让你知道
  • 抱歉,UIImageJPEGRepresentation 有 2 个参数。我更新了答案。
  • @Sam Corder Thaaaaaaanks.
【解决方案2】:

您可以获取所选图像的PNG字节并像这样上传它,而不是将其转换为NSURL:

storage.reference().child("images/imageName.png").putData(selectedImage!.pngData()!, metadata: nil, completion: {_, error in
                guard error == nil else{
                    print("failed to upload image")
                    return
                }
            })

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 2021-03-17
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    相关资源
    最近更新 更多