Skip to content

gchiacchio/AlamoImage

Repository files navigation

AlamoImage

Version License Platform

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 = "http://httpbin.org/image"
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 = "http://httpbin.org/image"
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 = "http://httpbin.org/image"
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 = "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
                                  )   
    }
)

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

UIImage and NSImage extensions

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.

Basic

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

Requirements

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

Installation through CocoaPods

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'

Author

Guillermo Chiacchio, guillermo.chiacchio@gmail.com

License

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

About

Image downloading with Alamofire

Resources

License

Stars

Watchers

Forks

Packages

No packages published