TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Oct 2015 |
SPMSupports SPM | ✗ |
Maintained by Guillermo Chiacchio.
import AlamoImage
at the top of your swift filelet imageView = UIImageView()
let imageView = NSImageView()
let imageURL = "https://avatars3.githubusercontent.com/u/7842501?v=3&s=40"
Alamofire.request(.GET, imageURL)
.responseImage() { (request, _, image, error) in
if error == nil && image != nil {
self.imageView.image = image
}
}
The simplest way to request an image is with just an URLStringConvertible
instance.
let imageURL = "https://avatars3.githubusercontent.com/u/7842501?v=3&s=40"
self.imageView.requestImage(imageURL)
You can also put a placeholder
image. This image will be in place while the request is not responded, or if it resulted in error.
let imageURL = "https://avatars3.githubusercontent.com/u/7842501?v=3&s=40"
let placeholder = UIImage(named:"smile.png") // In OSX use NSImage(named:"smile.png")
self.imageView.requestImage(imageURL, placeholder:placeholder)
If you want more control to handle the views, you can also use the success
and failure
closure parameters. Here is an example
let imageURL = "https://avatars3.githubusercontent.com/u/7842501?v=3&s=40"
let placeholder = UIImage(named:"smile.png") // In OSX use NSImage(named:"smile.png")
self.imageView.requestImage(imageURL,
placeholder: placeholder,
success:
{ (imageView, _, _, image) in
UIView.transitionWithView(imageView,
duration: 1.0,
options: .TransitionCrossDissolve,
animations: {
imageView.image = image
},
completion: nil
)
}
)
Every UIImageView
(NSImageView
in OSX) has his own reference to the last started request
. It is automatically cancelled every time a new request is made, but you can cancel
it at any moment.
self.imageView.request?.cancel()
Guillermo Chiacchio, [email protected]
AlamoImage is available under the MIT license. See the LICENSE file for more info.