Swift3 - UIImageView 未出现在已解析的 JSON 数据中
Swift3 - UIImageView isn't appearing from parsed JSON data
大家好,希望对我的第一个问题有所帮助。我花了很长时间浏览论坛,但仍在努力理解我哪里出了问题。 URL 字符串从 JSON 解析得很好,但是它没有在我创建的 UIImageView 中显示为图像。代码编译正常,其他一切都在显示。我假设我必须将类型为 String 的 URL 转换为 UIImageView 但是我不断收到错误。
这是 EventCell 的片段 class。
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
}()
这里是事件 class 本身的片段。
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;
}
}
如有任何帮助,我们将不胜感激!谢谢!
初始化程序 UIImage(named:)
需要已存储在包中的图像的名称。在您的例子中,您想从 URL
加载图像。您应该做的是从 String
创建一个 URL
,然后将 URL
的内容加载为 NSData
,最后从 UIImage
创建一个 UIImage
18=]。实现这一目标的最快方法是
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
如果你正在使用
根据 Apple 文档 UIImage(named:)
需要您的应用程序包中的名称。您可以在 dataWithContentsOfURL:
方法中传递图像的 url 。虽然使用这种方法很好,但您只需要异步处理,因为 dataWithContentsOfURL: 方法是一个同步请求,并会阻塞主线程,直到下载完成。
考虑使用 SDWebImage UIImageView 扩展,它添加了管理图像下载和显示的功能。它还支持您可以提供的占位符图像,以指示下载正在进行中。
希望对您有所帮助。编码愉快!!
添加此扩展程序
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")
大家好,希望对我的第一个问题有所帮助。我花了很长时间浏览论坛,但仍在努力理解我哪里出了问题。 URL 字符串从 JSON 解析得很好,但是它没有在我创建的 UIImageView 中显示为图像。代码编译正常,其他一切都在显示。我假设我必须将类型为 String 的 URL 转换为 UIImageView 但是我不断收到错误。
这是 EventCell 的片段 class。
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
}()
这里是事件 class 本身的片段。
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;
}
}
如有任何帮助,我们将不胜感激!谢谢!
初始化程序 UIImage(named:)
需要已存储在包中的图像的名称。在您的例子中,您想从 URL
加载图像。您应该做的是从 String
创建一个 URL
,然后将 URL
的内容加载为 NSData
,最后从 UIImage
创建一个 UIImage
18=]。实现这一目标的最快方法是
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
如果你正在使用
根据 Apple 文档 UIImage(named:)
需要您的应用程序包中的名称。您可以在 dataWithContentsOfURL:
方法中传递图像的 url 。虽然使用这种方法很好,但您只需要异步处理,因为 dataWithContentsOfURL: 方法是一个同步请求,并会阻塞主线程,直到下载完成。
考虑使用 SDWebImage UIImageView 扩展,它添加了管理图像下载和显示的功能。它还支持您可以提供的占位符图像,以指示下载正在进行中。
希望对您有所帮助。编码愉快!!
添加此扩展程序
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")