I'm trying to lazy load a property (UIImage) with an Alamofire request. This is where I'm at:
public lazy var theImage: UIImage = MyCalass.loadImage { (imageTest) -> Void in
return imageTest
}
class public func loadImage(completion: (imageTest: UIImage) -> Void){
Alamofire.request(.GET, "http://pathtoimage/image.jpg").response { (request, response, data, error) in
var tempImage = UIImage(data: data!, scale:1)
completion(imageTest: tempImage!)
}
}
Error on the : '()' is not convertible to 'UIImage'
Hey man its easy to solve with GCD...
Just build up a Class like this:
class AsynchImageLoader {
class func loadImageAsync (imageURL: NSURL, completionHandler: (downloadedImage: UIImage?) -> Void) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
if let imageData: NSData = NSData(contentsOfURL: imageURL) {
if let imageImage: UIImage = UIImage(data: imageData) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completionHandler(downloadedImage: imageImage)
})
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completionHandler(downloadedImage: nil)
})
}
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completionHandler(downloadedImage: nil)
})
}
})
}
}
And than load your Images like this...
let imageView: UIImageView = UIImageView()
AsynchImageLoader.loadImageAsync(NSURL(string: "yourURL")!, completionHandler: { (downloadedImage) -> Void in
imageView.image = downloadedImage
})
downloadedImage could be nil, if something went wrong... otherwise you could put it into your view, or an array, or whatever you want...
By the way... The lazy modifier doesn't mean that the image is loaded "lazy/async"... In Swift you can declare / initialize something as lazy what means that it will be declared on init of its class, but it will first be initialized shortly before you firstly try to access it in code... That could be any time, or even never :-)
Or change your Code from this...
public lazy var theImage: UIImage = MyCalass.loadImage { (imageTest) -> Void in
return imageTest
}
To this...
var theImage: UIImage?
MyCalass.loadImage { (imageTest) -> Void in
theImage = imageTest
}
If you're working with Images, I recommend you to use something like DLImageLoader, it can be installed with CocoaPods, for loading an image just use
DLImageLoader.sharedInstance().displayImageFromUrl("\(thumbnailUrl)", imageView: myImageView)
This way your image is loaded when the download has finished, also check the other options, there are some with callbacks, but the above example is the simplest
And it even uses cache if you need the image several times in your app, I have an implementation of it and works nice, and I have Alamofire also. I came to this complement once I replaced AFNetworking which used to have this kind of image download management
You don't have to use Alamofire to load in an image. You can make it async though.
You could do something like this, closure included:
lazy var theImage: UIImage = {
var sampImage = getImage({ (image) in
return image
})
}()
func getImage(completion: UIImage -> ()) {
var newImage: UIImage?
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let url = NSURL(string: "http://pathtoimage/image.jpg")
let data = NSData(contentsOfURL: url!)
newImage = UIImage(data: data!)!
completion(newImage!)
}
}
EDIT: I was able to successfully load an image in a Playground file using this code below. The problem before was that the request was async which for some reason lead to it not returning properly.
import UIKit
class Example {
lazy var theImage: UIImage = {
var newImage: UIImage?
let url = NSURL(string: "http://2.bp.blogspot.com/-XJbC3jA7cOo/VW2ItW5aRjI/AAAAAAAACcc/_f_WhTEXvFQ/s1600/testPattern.png")
let data = NSData(contentsOfURL: url!)
newImage = UIImage(data: data!)!
println(data)
return newImage!
}()
init() {}
}
var exp = Example()
exp.theImage
I'm thinking that function calls aren't allowed in lazy loaded properties
Related
I am a fairly decent Objective C developer, and I am now learning Swift (of which I am finding quite difficult, not only because of new concepts, such as optionals, but also because Swift is continually evolving, and much of the available tutorials are severely outdated).
Currently I am trying parse a JSON from a url into an NSDictionary and then use one of its value to display an image (which is also a url). Something like this:
URL -> NSDictionary -> init UIImage from url -> display UIImage in UIImageView
This is quite easy in Objective C (and there may even be a shorter answer):
NSURL *url = [NSURL URLWithString:#"https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"];
NSData *apodData = [NSData dataWithContentsOfURL:url];
NSDictionary *apodDict = [NSJSONSerialization JSONObjectWithData:apodData options:0 error:nil];
The above code snippet gives me back a standard NSDictionary, in which I can refer to the "url" key to get the address of the image I want to display:
"url" : "https://apod.nasa.gov/apod/image/1811/hillpan_apollo15_4000.jpg"
This I then convert into a UIImage and give it to a UIImageView:
NSURL *imageURL = [NSURL URLWithString: [apodDict objectForKey:#"url"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *apodImage = [UIImage imageWithData:imageData];
UIImageView *apodView = [[UIImageView alloc] initWithImage: apodImage];
Now, I am basically trying to replicate the above Objective C code in Swift but continuously run into walls. I have tried several tutorials (one of which actually did the exact same thing: display a NASA image), as well as find a few stack overflow answers but none could help because they are either outdated or they do things differently than what I need.
So, I would like to ask the community to provide the Swift 4 code for the these problems:
1. Convert data from url into a Dictionary
2. Use key:value pair from dict to get url to display an image
If it is not too much already, I would also like to ask for detailed descriptions alongside the code because I would like the answer to be the one comprehensive "tutorial" for this task that I believe is currently not available anywhere.
Thank you!
First of all I'm pretty sure that in half a year you will find Objective-C very complicated and difficult. 😉
Second of all even your ObjC code is discouraged. Don't load data from a remote URL with synchronous Data(contentsOf method. Regardless of the language use an asynchronous way like (NS)URLSession.
And don't use Foundation collection types NSArray and NSDictionary in Swift. Basically don't use NS... classes at all if there is a native Swift counterpart.
In Swift 4 you can easily decode the JSON with the Decodable protocol directly into a (Swift) struct,
the URL string can be even decoded as URL.
Create a struct
struct Item: Decodable {
// let copyright, date, explanation: String
// let hdurl: String
// let mediaType, serviceVersion, title: String
let url: URL
}
Uncomment the lines if you need more than the URL.
And load the data with two data tasks.
let url = URL(string: "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY")!
let task = URLSession.shared.dataTask(with: url) { (data, _, error) in
if let error = error { print(error); return }
do {
let decoder = JSONDecoder()
// this line is only needed if all JSON keys are decoded
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(Item.self, from: data!)
let imageTask = URLSession.shared.dataTask(with: result.url) { (imageData, _, imageError) in
if let imageError = imageError { print(imageError); return }
DispatchQueue.main.async {
let apodImage = UIImage(data: imageData!)
let apodView = UIImageView(image: apodImage)
// do something with the image view
}
}
imageTask.resume()
} catch { print(error) }
}
task.resume()
You can use this extension
extension UIImage {
public static func loadFrom(url: URL, completion: #escaping (_ image: UIImage?) -> ()) {
DispatchQueue.global().async {
if let data = try? Data(contentsOf: url) {
DispatchQueue.main.async {
completion(UIImage(data: data))
}
} else {
DispatchQueue.main.async {
completion(nil)
}
}
}
}
}
Using
guard let url = URL(string: "http://myImage.com/image.png") else { return }
UIImage.loadFrom(url: url) { image in
self.photo.image = image
}
Since image loading is a trivial and at the same time task which could be implemented in many different ways, I would recommend you to not "reinvent the wheel" and have a look to an image loading library such as Nuke, since it already covers most of the cases you might need during your development process.
It allows you to load and show image asynchronously into your view, using simple api:
Nuke.loadImage(with: url, into: imageView)
And also if you need - to specify how image should be loaded and presented:
let options = ImageLoadingOptions(
placeholder: UIImage(named: "placeholder"),
failureImage: UIImage(named: "failure_image"),
contentModes: .init(
success: .scaleAspectFill,
failure: .center,
placeholder: .center
)
)
Nuke.loadImage(with: url, options: options, into: imageView)
Create an UIIimageView Extension and the following code
extension UIImageView {
public func imageFromServerURL(urlString: String) {
self.image = nil
let urlStringNew = urlString.replacingOccurrences(of: " ", with: "%20")
URLSession.shared.dataTask(with: NSURL(string: urlStringNew)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error as Any)
return
}
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
self.image = image
})
}).resume()
}}
and
self.UploadedImageView.imageFromServerURL(urlString: imageURLStirng!)
I have just extended on vadian's answer, separated some concerns to clearly understand the basics. His answer should suffice.
First, you have to build your structure. This will represent the JSON structure you retrieved from the webservice.
struct Item: Codable {
let url, hdurl : URL,
let copyright, explanation, media_type, service_version, title : String
}
Then make you request methods. I usually create a separate file for it. Now, vadian mentioned about completion handlers. These are represented by escaping closures. Here, closure ()-> is passed on both functions and called having the decoded data as argument.
struct RequestCtrl {
func fetchItem(completion: #escaping (Item?)->Void) {
let url = URL(string: "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY")!
//URLSessionDataTask handles the req and returns the data which you will decode based on the Item structure we defined above.
let task = URLSession.shared.dataTask(with: url) { (data, _, _) in
let jsonDecoder = JSONDecoder()
if let data = data,
let item = try? jsonDecoder.decode(Item.self, from: data){
//jsonDecoder requires a type of our structure represented by .self and the data from the request.
completion(item)
} else {
completion(nil)
}
}
task.resume()
}
func fetchItemPhoto(usingURL url: URL, completion: #escaping (Data?)-> Void) {
let task = URLSession.shared.dataTask(with: url) { (data, _, _) in
if let data = data { completion(data) } else { completion(nil) }
}
task.resume()
}
}
Now in you ViewController, call your request and handle the execution of your closure.
class ViewController: UIViewController {
let requestCtrl = RequestCtrl()
override func viewDidLoad() {
super.viewDidLoad()
requestCtrl.fetchItem { (fetchedItem) in
guard let fetchedItem = fetchedItem else { return }
self.getPhoto(with: fetchedItem)
}
}
func getPhoto(with item: Item) {
requestCtrl.fetchItemPhoto(usingURL: item.url) { (fetchedPhoto) in
guard let fetchedPhoto = fetchedPhoto else { return }
let photo = UIImage(data: fetchedPhoto)
//now you have a photo at your disposal
}
}
}
These are not the best of practices since I am also still learning, so by all means do some research on topics especially closures, ios concurrency and URLComponents on Apple's documentation :)
you need to convert url into string and data to add in imageview
let imageURL:URL=URL(string: YourImageURL)!
let data=NSData(contentsOf: imageURL)
Yourimage.image=UIImage(data: data! as Data)
First add the pod in Podfile
pod 'Alamofire',
pod 'AlamofireImage'
you can check this link for install pods => https://cocoapods.org/pods/AlamofireImage
// Use this function for load image from URL in imageview
imageView.af_setImage(
withURL: url,
placeholderImage: placeholderImage //its optional if you want to add placeholder
)
Check this link for method of alamofireImage
https://github.com/Alamofire/AlamofireImage/blob/master/Documentation/AlamofireImage%203.0%20Migration%20Guide.md
Update for Xcode 13.3 , Swift 5
To load the Image asynchronously from a URL string, use this extension:
extension UIImageView {
public func getImageFromURLString(imageURLString: String) {
guard let imageURL = URL(string: imageURLString) else { return}
Task {
await requestImageFromURL(imageURL)
}
}
private func requestImageFromURL(_ imageURL: URL) async{
let urlRequest = URLRequest(url: imageURL)
do {
let (data, response) = try await URLSession.shared.data(for: urlRequest)
if let httpResponse = response as? HTTPURLResponse{
if httpResponse.statusCode == 200{
print("Fetched image successfully")
}
}
// Loading the image here
self.image = UIImage(data: data)
} catch let error {
print(error)
}
}
}
Usage:
imageView.getImageFromURLString(imageURLString: "https://apod.nasa.gov/apod/image/1811/hillpan_apollo15_4000.jpg")
I have upload the image from my application and when I want to download its not able to cache.
My url : https://workmate.mapmyindia.com/app/mobile/displayImage/3b9c509d-6461-455d-8a2a-461adc73b81e
If I replace the string with other url, it works with following code.
var imageUrl : String = "https://workmate.mapmyindia.com/app/mobile/displayImage/3b9c509d-6461-455d-8a2a-461adc73b81e"
if let image = WMRequestManager().cachedImage(for: imageUrl) {
cell.taskImageView.image = image
}
else {
WMRequestManager().downloadImage(imageURL: imageUrl, success: { (img) in
cell.taskImageView.image = img
})
}
// MARK:- Image Download
func downloadImage(imageURL: String, success: #escaping (_ response:UIImage?) -> Void) {
let urlString = imageURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
Alamofire.request(urlString!, method: .get).responseImage { response in
guard let image = response.result.value else {
// Handle error
success(nil)
return
}
self.cache(image, for: urlString!)
success(image)
}
}
//MARK: - Image Caching
let imageCache = AutoPurgingImageCache(
memoryCapacity: UInt64(100).megabytes(),
preferredMemoryUsageAfterPurge: UInt64(60).megabytes()
)
func cache(_ image: Image, for url: String) {
imageCache.add(image, withIdentifier: url)
}
func cachedImage(for url: String) -> Image? {
return imageCache.image(withIdentifier: url)
}
extension UInt64 {
func megabytes() -> UInt64 {
return self * 1024 * 1024
}
}
I am stuck with, what the issue it will be. Is it a scaling issue or url issue. Is it a server side issue or client side.
If you can try use KingFisher (or something similar) that does the caching of the image for you. It will make your life a lot easier in the long run and will avoid your whole problem.
Hi i want to cache my images coming from json. I have made a networkService which downloads and parse the json and i use the func downloadImage() which makes http request of the imageurl.Then I update my episode with this func but images are still downloading on scroll
var episode: Product! {
didSet {
self.updateUI()
}
}
let imageCache = NSCache<AnyObject, AnyObject>()
func updateUI()
{
menuItemNameLabel?.text = episode.title
ingredientsItemLabel?.text = episode.summary
priceItemLabel?.text = episode.price
menuItemImageView?.image = UIImage(named: "Koulourades")
if let thumbnailURL = episode.thumbnailURL {
let networkService = NetworkService(url: thumbnailURL)
networkService.downloadImage({ (imageData) in
if let imageFromCache = self.imageCache.object(forKey: self.episode.thumbnailURL as AnyObject) as? UIImage {
DispatchQueue.main.async(execute: {
self.menuItemImageView?.image = imageFromCache
return
})
}
DispatchQueue.main.async(execute: {
let imageToCache = UIImage(data: imageData as Data)
self.imageCache.setObject(imageToCache!, forKey: self.episode.thumbnailURL as AnyObject)
self.menuItemImageView?.image = imageToCache
})
})
}
}//--end updateUI()
The way you need to resolve your problem is called Lazy Loading in which application will download the images at once and cache it into the memory.
There are multiple third party libraries available for caching images.
Libraries like ,
1) HanekeSwift -> https://github.com/Haneke/HanekeSwift
2) Kingfisher -> https://github.com/onevcat/Kingfisher
3) SDWebImage -> https://github.com/rs/SDWebImage
4) AlamofireImage -> https://github.com/Alamofire/AlamofireImage
I have images in my collectionViewCell's that are fetched and parsed via NSURLRequest, how do I cache these images so they don't have to start a new request with every single appearance/disappearance of the view?
here is my code that fetches the images:
class funnyPicture: NSObject {
var pfPicture : PFObject
var coverImage : UIImage!
init(pfPicture: PFObject) {
self.pfPicture = pfPicture
}
func fetchCoverImage(completion: (image: UIImage?, error: NSError?) -> Void) {
let urlString = self.pfPicture["funnyPictures"] as! String
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
let queue = dispatch_get_main_queue()
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
if error == nil {
self.coverImage = UIImage(data: data!)
completion(image: self.coverImage, error: nil)
} else {
completion(image: nil, error: error)
}
}
}
}
and here is my collectionView code that parse the images to the collectionViewCell's:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
// Configure the cell
let book = self.books[indexPath.row]
let coverImage = book.coverImage
if coverImage == nil {
book.fetchCoverImage({ (image, error) -> Void in
if self.collectionView != nil {
collectionView.reloadItemsAtIndexPaths([indexPath])
}
})
} else {
dispatch_async(dispatch_get_main_queue()){
let imageView = cell.imageView
imageView.image = book.coverImage
}
};
if book.coverImage == nil {
cell.imageView.userInteractionEnabled = false
cell.userInteractionEnabled = false
}else {
cell.imageView.userInteractionEnabled = true
cell.userInteractionEnabled = true
}
return cell
}
While I've received references to third party frameworks, I haven't received any answer on how to implement them with the code I have provided in the question, or even an answer using apples already implemented caching mechanism.. The reason I put the code in the question was for use in an answer.. Thank you.
Here is an example for your collection view cell:
import UIKit
let imageCache = NSCache<AnyObject, AnyObject>.sharedInstance
class myCell: UICollectionViewCell {
#IBOutlet public weak var myImageView: UIImageView?
private var imageUrlString: String?
private var downloadTask: URLSessionDownloadTask?
public var imageURL: URL? {
didSet {
self.downloadItemImageForSearchResult(imageURL: imageURL)
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
public func downloadItemImageForSearchResult(imageURL: URL?) {
if let urlOfImage = imageURL {
if let cachedImage = imageCache.object(forKey: urlOfImage.absoluteString as NSString){
self.myImageView!.image = cachedImage as? UIImage
} else {
let session = URLSession.shared
self.downloadTask = session.downloadTask(
with: urlOfImage as URL, completionHandler: { [weak self] url, response, error in
if error == nil, let url = url, let data = NSData(contentsOf: url), let image = UIImage(data: data as Data) {
DispatchQueue.main.async() {
let imageToCache = image
if let strongSelf = self, let imageView = strongSelf.myImageView {
imageView.image = imageToCache
imageCache.setObject(imageToCache, forKey: urlOfImage.absoluteString as NSString , cost: 1)
}
}
} else {
//print("ERROR \(error?.localizedDescription)")
}
})
self.downloadTask!.resume()
}
}
}
override public func prepareForReuse() {
self.downloadTask?.cancel()
myImageView?.image = UIImage(named: "ImagePlaceholder")
}
deinit {
self.downloadTask?.cancel()
myImageView?.image = nil
}
}
Don't forget to make an extension for NSCache
Like this:
import Foundation
extension NSCache {
class var sharedInstance: NSCache<NSString, AnyObject> {
let cache = NSCache<NSString, AnyObject>()
return cache
}
}
Use NSCache and NSOperationQueue to manage your image loading. There's a good post outlining the technique at https://stackoverflow.com/a/12721899/5271191 (It's Objective-C, but the technique is the same for Swift.)
I highly recommend you to use a clean in place replacement/extension for UIImageView, that will manage caching of the image all transparently to you and avoid unwanted complexity of maintaining operation queues, etc.
If in memory caching suffices your needs - check this out-
https://github.com/nicklockwood/AsyncImageView
If you want persistent caching, then this one will do-
https://github.com/rs/SDWebImage
HTH.
I have images in my collectionViewCell's that are fetched and parsed
via NSURLRequest, how do I cache these images so they don't have to
start a new request with every single appearance/disappearance of the
view?
The URL loading system already provides a cache. Take a look at the docs for NSURLCache. If the resources you need aren't already being sufficiently cached, you probably only need to adjust the disk space allocated to the URL cache for your app.
You should also take a look at the headers (cache-control, expires, etc.) that come back with your resources to make sure that they're not preventing caching. Here's a short tutorial on cache-related headers.
You should use a specialized framework for that. I would not recommend using SDWebImage, it is very outdated and is not stable.
Take a look at those two libraries that are up to date with iOS platform:
DFImageManager - advanced framework written in Objective-C but featuring nullability annotations (works great with Swift). Here's a list of things that make it better, than SDWebImage. Disclosure: it's written by me, opinion might be biased.
Kingfisher - lightweight library written in Swift. Similar to SDWebImage, but has much less features that SDWebImage and DFImageManager.
I have created a library using swift 2 to do the request for image and cache it. it's very simple just give it a try.
https://github.com/georgehadly/GHImageCaching
all you can do is something like this ,
viewImg.getCachedImage("geo", URI: NSURL(string: "https://s-media-cache-ak0.pinimg.com/236x/8e/5a/98/8e5a98795dc2c5322cac97343a6cad6d.jpg")!) { (done) -> Void in
if(done){
// your extra
}
}
in case you want to delete all cached images
UIImageView.deleteAllCaching()
I need to load an image from a url and set it inside an UIImageView; the problem is that I don't know the exact size of the image, then how can I show the image correctly?
Just use the size property of UIImage, for example:
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
CGSize size = img.size;
In swift:
var url = NSURL.URLWithString("http://www.example.com/picture.png")
var data = NSData(contentsOfURL : url)
var image = UIImage(data : data)
image.size // if you need it
In swift regarding using optionals:
var url:NSURL? = NSURL(string: imageString)
var data:NSData? = NSData(contentsOfURL : url!)
var image = UIImage(data : data!)
IN SWIFT 3.0
The main thread must be always remain free so it serves the user interface and user interactions.
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
private func fetchImage() {
let imageURL = URL(string: "https://i.stack.imgur.com/9z6nS.png")
var image: UIImage?
if let url = imageURL {
//All network operations has to run on different thread(not on main thread).
DispatchQueue.global(qos: .userInitiated).async {
let imageData = NSData(contentsOf: url)
//All UI operations has to run on main thread.
DispatchQueue.main.async {
if imageData != nil {
image = UIImage(data: imageData as! Data)
self.imageView.image = image
self.imageView.sizeToFit()
} else {
image = nil
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
fetchImage()
}
}
To download Asynchronous image with Kingfisher library you can follow this step,url :https://github.com/onevcat/Kingfisher:
func imageFromUrl(_ urlString: String) {
if let url = URL(string: urlString) {
ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) {
(image, error, url, data) in
DispatchQueue.main.async {
self.imageView.image = image
}
}
}
}
You can also download image with default URLSession.shared.dataTask
func imageFromUrl(_ urlString: String) {
if let url = URL(string: urlString) {
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) {(data,response,error) in
if let imageData = data as Data? {
if let img = UIImage(data: imageData){
DispatchQueue.main.async {
self.imageView.image = img
}
}
}
}
}
}
Swift safe code version:
private func loadImage(from url:URL) -> UIImage? {
let imageData: Data
do {
imageData = try Data(contentsOf: url)
} catch {
return nil
}
return UIImage(data: imageData)
}
private func loadImage(from urlString:String) -> UIImage? {
guard let url = URL(string: urlString) else {
return nil
}
return self.loadImage(from: url)
}
Keep in mind that this code blocks the main thread, so you should run it on a background thread. For example:
DispatchQueue.global().async {
let image = UIImage(fromFile: "http://xpto.com/image.png")
// And then you should update UI on main thread.
// If you have an UIImageView outlet you can update its image this way:
DispatchQueue.main.async {
imageView.image = image
imageView.contentMode = .scaleAspectFit
}
}
SWIFT 5.0 + fetch on background
private func fetchImage(_ photoURL: URL?) {
guard let imageURL = photoURL else { return }
DispatchQueue.global(qos: .userInitiated).async {
do{
let imageData: Data = try Data(contentsOf: imageURL)
DispatchQueue.main.async {
let image = UIImage(data: imageData)
self.userImageView.image = image
self.userImageView.sizeToFit()
self.tableView.reloadData()
}
}catch{
print("Unable to load data: \(error)")
}
}
}
One common mistake in displaying an image downloaded from json or a url is the problem of queues. ALL UI-related things need to be done in the main queue, so if you forgot this, even perfect code (above answers are good) won't display your image on occasion. To call the mainQueue, use code like this, and note that calling main queue might need to be done seperately in the called imageDisplay function:
dispatch_async(dispatch_get_main_queue(), ^{
self.nameLabel.text = self.pokemon.name;
[self displayImage]; //CALLS FUNCTION
self.abilitiesTextView.text = #"loves SwiftUI";
});
- (void)displayImage {
NSString *imageURLString = [NSString stringWithFormat: self.pokemon.sprite];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURLString]];
dispatch_async(dispatch_get_main_queue(), ^{
self.spriteImageView.image = [UIImage imageWithData: imageData];
});
// NOTE: important for newer versions of XCode/Obj-C...
//[imageData release]; With ARC ("automated release..."), release method is forbidden, it's already done for you.
}
If you prefer you can even move it to an UIImage extension:
extension UIImage {
//NOTE: This is not thread safe, please run it on a background thread.
convenience init?(fromFile filePath:String) {
guard let url = URL(string: filePath) else {
return nil
}
self.init(fromURL: url)
}
//NOTE: This is not thread safe, please run it on a background thread.
convenience init?(fromURL url:URL) {
let imageData: Data
do {
imageData = try Data(contentsOf: url)
} catch {
return nil
}
self.init(data: imageData)
}
}