コード間違ってたらすいません。
What is Alamofire and AlamofireImage?
Alamofire is an HTTP networking library written in Swift, and AlamofireImage which is an image component library for Alamofire.
Why do we have to use AlamofireImage to retrieve an image?
Some people have a setback in retrieving image and using cache, so I highly recommend you to use Image Downloader from AlamofireImage. As you know, if iPhone retrieves image data and stores in it, those images will be used a lot of memory. Therefore, we have to use cache to deal it. Image Downloader includes Cache function. It means we do not need to spend a time with coding cache.
Development Environment
- OS X El Captain 10.11.2
- Xcode Version 8.0
Language
Swift 2.3
Code
ViewController.swift
import UIKit
import AlamofireImage
class ViewController: UIViewController {
@IBOutlet weak var profileImage: UIImageView!
let downloader = ImageDownloader()
override func viewDidLoad() {
super.viewDidLoad()
retrieveProfileImage()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController{
func retrieveProfileImage(){
// Set Image URL
let urlString = "http://~~"
// Display Profile Image (downloader function includes cache function)
let URLRequest = NSURLRequest(URL: NSURL(string: urlString!)!)
downloader.downloadImage(URLRequest: URLRequest) { response in
if let image = response.result.value {
self.profileImage.image = image
}
}
}
}
Conclusion
超簡単かつ便利。