When I try to load an image from a URL I always get an error despite the fact that I know that there is a URL that contains a valid .jpg image.
mainImageView.image = try UIImage(data: Data(contentsOf: URL(fileURLWithPath: artworkURL)))
I also tried these solution but the line where I initialise the UIImage
still throws an error.
Example of data in artworkURL
:
Current solutions do not solve my problem. I also tried:
try UIImage(data: Data(contentsOf: URL(string: artworkURL)!))
and
try UIImage(data: Data(contentsOf: (URL : artworkURL)))
and (fuller code) now using a static url to debug
let artworkString = "http://is1.mzstatic.com/image/thumb/Music/v4/ef/97/95/ef979538-8321-151a-dceb-9b0a7f7c7641/source/100x100bb.jpg" // album.artworkURL[index.row]
let url = URL(string: artworkString)
do {
mainImageView.image = try UIImage(data: Data(contentsOf: url!))
} catch _ {
print("No album artwork!")
}
This last one outputs "No album artwork!"
!