AlamoImage 0.3.7

AlamoImage 0.3.7

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2015
SPMSupports SPM

Maintained by Guillermo Chiacchio.



Usage

  1. Put import AlamoImage at the top of your swift file
  2. Have a place to put the image
    iOS:
    let imageView = UIImageView()

    OSX:
    let imageView = NSImageView()

  3. Use any of the following methods:

Requesting an image

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
         }
}  

UIImageView and NSImageView extensions

Basic

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)

Placeholder

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)

Handling results

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
                                  )   
    }
)

Canceling

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()

Requirements

  • iOS 8.0+ / Mac OS X 10.9+
  • Xcode 6.3

Author

Guillermo Chiacchio, [email protected]

License

AlamoImage is available under the MIT license. See the LICENSE file for more info.