Loading an Image into Uiimage Asynchronously

Loading an image into UIImage asynchronously

Take a look at SDWebImage:

https://github.com/rs/SDWebImage

It's a fantastic set of classes that handle everything for you.

Tim

How to load image asynchronously with Swift using UIImageViewExtension and preventing duplicate images or wrong Images loaded to cells

You are asynchronously updating your image view, regardless of whether the image view has been re-used for another cell.

When you start a new request for an image view, assuming you didn’t find an image in the cache immediately, before starting network request, you should (a) remove any prior image (like Brandon suggested); (b) possibly load a placeholder image or UIActivityIndicatorView; and (c) cancel any prior image request for that image view. Only then should you start a new request.

In terms of how you save a reference to the prior request in an extension, you can’t add stored properties, but you can use objc_setAssociatedObject to save the session task when you start the session, set it to nil when the request finishes, and objc_getAssociatedObject when retrieving the session object to see if you need to cancel the prior one.

(Incidentally, Kingfisher wraps this associated object logic in their computed property for the task identifier. This is a fine way to save and retrieve this task identifier.


In terms of failed requests, the fact that you are performing unbridled image requests could cause that problem. Scroll around a bit and your requests will get backlogged and timeout. Doing the cancelation (see above) will diminish that problem, but it might still eventually happen. If you continue to have requests fail after fixing the above, then you might need to constrain the number of concurrent requests. A common solution is to wrap requests in asynchronous Operation subclass and add them to OperationQueue with a maxConcurrentOperationCount. Or if you’re looking for a cheap and cheerful solution, you could try bumping up the timeout threshold in your requests.

How to load image asynchronously using UIImageView+AFNetworking in ios

You can use SDWebImage

It supports asynchronous downloading as well as caching.

Usage

Just #import the UIImageView+WebCache.h header

  [imgView sd_setImageWithURL:Url_Of_The_Image placeholderImage:[UIImage imageNamed:@"Sampleimage.png"]];

Load local images into an array asynchronously on iOS

You can create a callback and call it after loading the images on the background thread, since the images will be loaded synchronously there.

I recommend doing something like this:

func loadImages(callback: @escaping ([UIImage]) -> Void) {
DispatchQueue.global(qos: .background).async {
let array = (0..<200).flatMap({ UIImage(named: "\($0).png") })
callback(array)
}
}

Which can be used like this (using swift trailing closure syntax):

loadImages { images in
// do something with the images
}

You might have to dispatch the callback(array) back to the DispatchQueue.main.

How to asynchronous load image from a web-server in UICollectionView using NSCache

Try this one it's Working code (Swift 4).

func NKPlaceholderImage(image:UIImage?, imageView:UIImageView?,imgUrl:String,compate:@escaping (UIImage?) -> Void){

if image != nil && imageView != nil {
imageView!.image = image!
}

var urlcatch = imgUrl.replacingOccurrences(of: "/", with: "#")
let documentpath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
urlcatch = documentpath + "/" + "\(urlcatch)"

let image = UIImage(contentsOfFile:urlcatch)
if image != nil && imageView != nil
{
imageView!.image = image!
compate(image)

}else{

if let url = URL(string: imgUrl){

DispatchQueue.global(qos: .background).async {
() -> Void in
let imgdata = NSData(contentsOf: url)
DispatchQueue.main.async {
() -> Void in
imgdata?.write(toFile: urlcatch, atomically: true)
let image = UIImage(contentsOfFile:urlcatch)
compate(image)
if image != nil {
if imageView != nil {
imageView!.image = image!
}
}
}
}
}
}
}

Use Like this :

// Here imgPicture = your imageView and UIImage(named: "placeholder") is Display image brfore download actual image.  
imgPicture.image = nil
NKPlaceholderImage(image: UIImage(named: "placeholder"), imageView: imgPicture, imgUrl: "Put Here your server image Url Sting") { (image) in }


Related Topics



Leave a reply



Submit