- Put
import AlamoImage
at the top of your swift file - Have a place to put the image
iOS:let imageView = UIImageView()
OSX:let imageView = NSImageView()
- Use any of the following methods:
let imageURL = "http://httpbin.org/image"
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 = "http://httpbin.org/image"
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 = "http://httpbin.org/image"
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 = "http://httpbin.org/image"
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()
UIImageView and NSImageView extensions should be enough most of the time. But in some cases you may need to request an UIImage or NSImage without having an UIImageView or NSImageView instance.
The simplest way to request an image is with just an URLStringConvertible
instance.
// inside some class
var photo: UIImage? // In OSX use NSImage
let imageURL = "http://httpbin.org/image"
UIImage.requestImage(imageURL){self.photo = $0} // In OSX use NSImage
- iOS 8.0+ / Mac OS X 10.9+
- Xcode 6.3
AlamoImage is available through CocoaPods. To install it, simply update your Podfile to match the following (note that AlamoImage depends on Alamofire):
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'Alamofire', '~> 1.2'
pod 'AlamoImage'
# Include the following only if you want to use UIImageView (NSImageView) extensions with AlamoImage
pod 'AlamoImage/ImageView'
Guillermo Chiacchio, [email protected]
AlamoImage is available under the MIT license. See the LICENSE file for more info.