Ben
Ben

Reputation: 86

How to load UIImage from url?

I have the following problem:

fatal error: unexpectedly found nil while unwrapping an Optional value

@IBOutlet weak var DetailImageView1: UIImageView!
@IBOutlet weak var DetailLabel: UILabel!

@IBOutlet weak var DetailLabel2: UILabel!

var SentData1: String!
var SentData2: String!
var SentData3: NSURL!

 override func viewDidLoad() {
    super.viewDidLoad()



    DetailLabel.text = SentData1
    DetailLabel2.text = SentData2
    let url = NSURL(string: "\(SentData3)")
    let data = NSData(contentsOfURL: url!)
    DetailImageView1.image = UIImage(data: data!)

I am taking the picture from a url and segue the url link from my previous view controller to this one. Then I created SentData3: NSURL! Now I have to show the picture in the DetailImageView1.image, but when I try to test the app I get an error.

I would be glad if someone can show me the mistake.

Upvotes: 2

Views: 8945

Answers (3)

Dakata
Dakata

Reputation: 1355

You can try that for SwiftUI:

import SwiftUI

struct RemoteImage: View {
private enum LoadState {
    case loading, success, failure
}

private class Loader: ObservableObject {
    var data = Data()
    var state = LoadState.loading

    init(url: String) {
        guard let parsedURL = URL(string: url) else {
            fatalError("Invalid URL: \(url)")
        }

        URLSession.shared.dataTask(with: parsedURL) { data, response, error in
            if let data = data, data.count > 0 {
                self.data = data
                self.state = .success
            } else {
                self.state = .failure
            }

            DispatchQueue.main.async {
                self.objectWillChange.send()
            }
        }.resume()
    }
}

@StateObject private var loader: Loader
var loading: Image
var failure: Image

var body: some View {
    selectImage()
        .resizable()
}

init(url: String, loading: Image = Image(systemName: "photo"), failure: Image = Image(systemName: "multiply.circle")) {
    _loader = StateObject(wrappedValue: Loader(url: url))
    self.loading = loading
    self.failure = failure
}

private func selectImage() -> Image {
    switch loader.state {
    case .loading:
        return loading
    case .failure:
        return failure
    default:
        if let image = UIImage(data: loader.data) {
            return Image(uiImage: image)
        } else {
            return failure
        }
    }
}
}

Then use it in your view:

struct ContentView: View {
let jsonURL = "https://cf.geekdo-images.com/thumb/img/sD_qvrzIbvfobJj0ZDAaq-TnQPs=/fit-in/200x150/pic2649952.jpg"

var body: some View {
    RemoteImage(url: jsonURL)
        .aspectRatio(contentMode: .fit)
        .frame(width: 200)
}
}

Upvotes: 1

Andrew
Andrew

Reputation: 184

This works for me...

var image: UIImage?
let urlString = "https://example.com/filename"

let url = NSURL(string: urlString)! as URL
if let imageData: NSData = NSData(contentsOf: url) {
    image = UIImage(data: imageData as Data)
}

..."image", will either be nil if there was an error, or it will contain the new UIImage object.

Upvotes: 8

Dylan Law
Dylan Law

Reputation: 327

If SentData3 is already a URL, you can just simply insert it. Don't force unwrap a variable when you are not sure whether it will return nil or not.

if let imageData: NSData = NSData(contentsOfURL: SentData3) {
    DetailImageView1.image = UIImage(data: imageData)
}

Upvotes: 0

Related Questions