【问题标题】:Converting URL to UIImage. Able to convert but not append to UIImage array将 URL 转换为 UIImage。能够转换但不能附加到 UIImage 数组
【发布时间】:2018-04-30 17:17:54
【问题描述】:

我正在使用 swift 创建一个应用程序,该应用程序调用 Google Places api 以生成位置的 JSON 文件,包括生成的位置的图像。这些图像以 URL 的形式给出,我需要将其转换为 UIImage,然后将这些图像附加到数组中。打开 URL 的内容时,我可以看到图像,但这些图像没有附加到数组中。这是试图生成所述图像的视图控制器的类:

import Foundation
import UIKit
import WebKit

class ImageViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    var photos: [Photo]?
    var uiImages: [UIImage]?

    override func viewDidLoad() {

        for photo in photos! {

            let url = URL(string:"https://maps.googleapis.com/maps/api/place/photo?photoreference=\(photo.reference)&sensor=false&maxheight=\(photo.height)&maxwidth=\(photo.width)&key=AIzaSyC_SoYT7VnYnyz3GAb7qqbXjZeLFG5GE70")

            let data = try? Data(contentsOf: url!)

            let image: UIImage = UIImage(data: data!)!

            self.uiImages?.append(image)

            print(image)

            print(self.uiImages)

        }
    }
}

在这个循环中,我告诉代码打印“image”,然后在追加发生后打印数组“uiImages”。然而,我在打印图像数组时返回 nil,但图像本身不返回 nil。

我觉得这可能与方法的异步性有关,但我也尝试在主线程上附加,但这并没有改变任何东西。另外,"photos" 变量不是 nil,它是在实例化视图控制器时设置的。

这是 Photo 类的代码:

import Foundation

struct Photo {

    var height: Int
    var width: Int
    var reference: String

    init?(height: Int, width: Int, reference: String) {
        self.height = height
        self.width = width
        self.reference = reference
    }
}

编辑:

在进行建议的更改后,我的 ImageViewController 类如下所示:

import Foundation
import UIKit
import WebKit

class ImageViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    var photos: [Photo]?
    var uiImages = [UIImage]()

    override func viewDidLoad() {

        for photo in photos! {

            let url = URL(string:"https://maps.googleapis.com/maps/api/place/photo?photoreference=\(photo.reference)&sensor=false&maxheight=\(photo.height)&maxwidth=\(photo.width)&key=AIzaSyC_SoYT7VnYnyz3GAb7qqbXjZeLFG5GE70")

            let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in

                let image: UIImage = UIImage(data: data!)!

                self.uiImages.append(image)

                print(image)

                print(self.uiImages)
            }

            task.resume()
        }
    }
}

【问题讨论】:

    标签: ios swift url asynchronous uiimage


    【解决方案1】:

    你永远不会初始化你的数组,你只是声明它。

    变化:

    var uiImages: [UIImage]?
    

    到:

    var uiImages = [UIImage]()
    

    然后改变:

    self.uiImages?.append(image)
    

    到:

    self.uiImages.append(image)
    

    附带说明,切勿使用Data(contentsOf:) 加载远程数据。使用URLSessiondataTask。由于主队列上的远程数据访问速度较慢,您的代码将导致各种问题。

    【讨论】:

    • 我不得不说,这是我喜欢的答案。很多人会评论“不要那样做!”没有回答问题。您实际上解决了 OP 的问题并提供了有用的批评作为脚注。太棒了。
    • 啊,当然。不能附加到不存在的东西上。非常感谢。
    猜你喜欢
    • 2019-11-15
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多