[][carthage]
[
][cocoadocs]
[
][cocoadocs]
[
][cocoadocs]
[cocoadocs]: http://cocoadocs.org/docsets/moa
[carthage]: https://github.com/Carthage/Carthage
Moa is an image download library written in Swift for iOS and OS X.
It allows to download and show an image in an image view by setting its moa.url property.
- Images are downloaded asynchronously.
- Uses NSURLSession for networking and caching.
- Allows to configure cache size and policy.
- Can be used without an image view.
- Provides closure properties for image manipulation and error handling.
- Includes unit testing mode for faking network responses.
"Lost, like the Moa is lost" - Maori proverb
'Hunting Moa' drawing by Joseph Smit (1836-1929). File source: Wikimedia Commons.
There are three ways you can add Moa to your Xcode project.
Add source (iOS 7+)
Simply add MoaDistrib.swift file into your Xcode project.
Setup with Carthage (iOS 8+)
Alternatively, add github "evgenyneu/moa" ~> 2.0 to your Cartfile and run carthage update.
Setup with CocoaPods (iOS 8+)
If you are using CocoaPods add this text to your Podfile and run pod install.
use_frameworks!
pod 'moa', '~> 2.0'
Moa is written in Swift 2 for Xcode 7. See Swift 1.2 setup instuctions for Xcode 6 projects.
-
Add
import moato your source code if you used Carthage or CocoaPods setup methods. -
Drag an Image View to your view in the storyboard. Create an outlet property for this image view in your view controller. Alternatively, instead of using the storyboard you can create a
UIImageViewobject in code. -
Set
moa.urlproperty of the image view to start asynchronous image download. The image will be automatically displayed when download is finished.
imageView.moa.url = "https://site.com/image.jpg"If your image URLs are not https you will need to and an exception to the Info.plist file. This will allow the App Transport Security to load the images from insecure HTTP hosts.
Ongoing image download for the image view is automatically canceled when:
- Image view is deallocated.
- New image download is started:
imageView.moa.url = ....
Call imageView.moa.cancel() to manually cancel the download.
You can supply an error image that will be used if an error occurs during image download.
imageView.moa.errorImage = UIImage(named: "ImageNotFound.jpg")
imageView.moa.url = "http://site.com/image.jpg"Alternatively, one can supply a global error image that will be used for all failed image downloads.
Moa.errorImage = UIImage(named: "ImageNotFound.jpg")Assign a closure that will be called when image is received.
imageView.moa.onSuccess = { image in
return image
}
imageView.moa.url = "http://site.com/image.jpg"- The closure will be called after download finishes and before the image is assigned to the image view.
- This is a good place to manipulate the image before it is shown.
- The closure returns an image that will be shown in the image view. Return nil if you do not want the image to be shown.
- The closure as called in the main queue. Use
onSuccessAsyncproperty instead if you need to do time consuming operations. - When
errorImageis supplied and an error occurs the success closures are called.
imageView.moa.onError = { error, response in
// Handle error
}
imageView.moa.url = "http://site.com/image.jpg"- The closure is called if image download fails. See Wiki for the list of possible error codes.
- The closure as called in the main queue. Use
onErrorAsyncproperty instead if you need to do time consuming operations.
An instance of Moa class can also be used without an image view. A strong reference to Moa instance needs to be kept.
let moa = Moa()
moa.onSuccess = { image in
// image is loaded
return image
}
moa.url = "http://site.com/image.jpg"Use the Moa.settings.cache to change caching settings. For more information please refer to the moa image caching manual.
// By default images are cached according to their response HTTP headers.
Moa.settings.cache.requestCachePolicy = .UseProtocolCachePolicy
// Always cache images locally regardless of their response HTTP headers
Moa.settings.cache.requestCachePolicy = .ReturnCacheDataElseLoadSometimes it is useful to prevent code from making real HTTP requests. Moa includes MoaSimulator class for testing image downloads and faking network responses. See unit test manual for more information.
// Autorespond with the given image to all image requests
MoaSimulator.autorespondWithImage("www.site.com", image: UIImage(named: "35px.jpg")!)The demo iOS app shows how to load images in a collection view with Moa.
Here is the list of other image download libraries for Swift.
- cbot/Vincent
- daltoniam/Skeets
- Haneke/HanekeSwift
- hirohisa/ImageLoaderSwift
- natelyman/SwiftImageLoader
- onevcat/Kingfisher
- zalando/MapleBacon
-
Demo app includes other drawings by Joseph Smit. Source: Wikimedia Commons.
-
OS X support is added by phimage.
Moa is released under the MIT License.

