0

嘿,伙计们会喜欢我的第一个问题的帮助。我花了很长时间浏览论坛,但仍在努力理解我在哪里出错了。URL 字符串从 JSON 中解析得很好,但是它没有在我创建的 UIImageView 中显示为图像。代码编译正常,其他一切都在显示。我假设我必须将 String 类型的 URL 转换为 UIImageView 但是我不断收到错误。

这是 EventCell 类的片段。

class EventCell: UICollectionViewCell {

var event: Event?   {
    didSet  {

        if let eventName = event?.title {
            eventTitle.text = eventName
        }

        eventSubtitle.text = event?.subtitle

        eventTags.text = event?.tags

        if let imageName = event?.imageURL
        {
            imageView.image = UIImage(named: imageName)
        }
    }
}

override init(frame: CGRect)    {
    super.init(frame: frame)

    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


let imageView: UIImageView =  {
    let iv = UIImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.contentMode = .scaleAspectFill
    return iv
}()

这是 Event 类本身的片段。

var id: NSNumber?
var title: String?
var imageURL: String?
var subtitle: String?
var tags: String?
var desc: String?

override init(){}

init(event: NSDictionary)
{
    if let val = event["ID"] as? NSNumber
    {
        id = val;
    }

    if let val = event["Title"] as? String
    {
        title = val;
    }

    if let val = event["Subtitle"] as? String
    {
        subtitle = val;
    }

    if let val = event["Tags"] as? String
    {
        tags = val;
    }

    if let val = event["Description"] as? String
    {
        desc = val;
    }

    if let val = event["image"] as? String
    {
        imageURL = val;
    }
}

任何帮助将非常感激!谢谢!

4

3 回答 3

2

初始化程序UIImage(named:)需要已经存储在包中的图像的名称。在您的情况下,您想从URL. 您应该做的是URL从. 实现这一目标的最快方法是StringURLNSDataUIImageData

if let imageURLString = event?.imageURL {
    let imageURL = URL(string: imageURLString!)
    let imageData = NSData(contentsOf: imageURL!)
    imageView.image = UIImage(data: imageData as! Data)
}

笔记

需要考虑的几点

  • 这不是从URL. 您还应该查看async请求。但是,这是最快的实现,应该可以使代码正常工作
  • 该片段在Swift 3语法中。我假设您可以将其转换为Swift 2您正在使用的
于 2017-06-27T03:19:38.873 回答
0

As per Apple documentation UIImage(named:) expects name of the name in your app bundle. You can pass the url of your image in dataWithContentsOfURL: method. Though using this approach is fine, you just need to process asynchronously as approach with dataWithContentsOfURL: is a synchronous request and blocks the main thread until that download is complete.

Consider using SDWebImage an UIImageView extension, it adds to manage the image download and display. It also supports a placeholder image that you can supply to indicate that the download is in progress.

Hope it helps. Happy Coding!!

于 2017-06-27T03:47:35.683 回答
0

添加这个扩展

extension UIImageView {
    public func imageFromUrl(urlString: String) {
        if let url = URL(string: urlString) {
            URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
                if (error != nil) {
                    print(error?.localizedDescription ?? "no errror")
                }
                else {
                    if let image = UIImage(data: data!) {
                        DispatchQueue.main.async {
                            self.image = image
                        }
                    }
                }
            }).resume()
        }
    }
}

用法 :

imageView.imageFromUrl(urlString: "http://linktoyourimage.com")
于 2017-06-27T05:26:52.033 回答