Fast lazy loading Alamofire request

I am trying to lazily load a property (UIImage) with an Alamofire request. This is where I am:

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 at: '()' does not convert to 'UIImage'

+3


source to share


3 answers


Hey, it's easy to solve with GCD ...

Just create 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 upload your images like this ...

let imageView: UIImageView = UIImageView()
        AsynchImageLoader.loadImageAsync(NSURL(string: "yourURL")!, completionHandler: { (downloadedImage) -> Void in
            imageView.image = downloadedImage
        })

      

downloadImage can be null if something went wrong ... otherwise you could put it in your view or array or whatever ...



By the way, a lazy modifier does not mean that the image is loaded "lazy / asynchronous" ... In Swift you can declare / initialize something lazy, which means that it will be declared when initializing its class, but first it will be initialized shortly before how would you first try to access it in code ... It 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
}

      

For this...

var theImage: UIImage? 
MyCalass.loadImage { (imageTest) -> Void in
    theImage = imageTest
}

      

+2


source


If you are working with images, I recommend you use something like DLImageLoader , it can be installed using CocoaPods, to load the image just use

DLImageLoader.sharedInstance().displayImageFromUrl("\(thumbnailUrl)", imageView: myImageView)

      



This way your image is loaded when the download completes and also checks other parameters, there are some with callbacks, but the example above is the simplest

And it even uses a cache if you need the image multiple times in your application, I have an implementation of it and it works well and I have Alamofire. I came to this add-on as soon as I replaced AFNetworking which had such image upload control

0


source


You don't need to use Alamofire to load the image. You can make it asynchronous.

You can do something like this, including closing:

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 the image in the Playground file using this code below. Previously, the problem was that the request was asynchronous, which for some reason resulted in an incorrect return.

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 think function calls are not allowed on lazy loaded properties

0


source







All Articles