如何使用Alamofire快速加载图像 [英] How to load image in swift using Alamofire

查看:69
本文介绍了如何使用Alamofire快速加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合视图控制器,该控制器通过URL加载图像异步。
(有点像Instegram)
我正在寻找实现加载图像部分的最佳方法。
请告诉我您的想法

I have a collection view controller, which load image Async by URL. (Something like Instegram) I am looking for the best way to implement the part of the loading image. please tell me what do you think

第一种方法-没有任何外部库:

First way - without any external library:

   let downloadQueue = dispatch_queue_create("com.pro.asyncImages",nil)
    dispatch_async(downloadQueue){

        var data = NSData(contentsOfURL: NSURL(string: pictureUrl!)!)

        var image: UIImage?
        if (data != nil){
            image = UIImage(data: data!)
        }
        dispatch_async(dispatch_get_main_queue()){
            uiImageView.image = image
        }
    }

第二种方式-使用Alamofire(请求属于Alamofire )

Second way - using Alamofire (the request belong to Alamofire)

if let  mediaUrl = info.mediaUrl {
        request(.GET,mediaUrl).response(){
            (_, _, data, _) in
            let image = UIImage(data: data! as NSData)
            imageView.image = image
        }
    }

最后,我读到AFNetworking在将url异步加载到urlImage方面做得很好。我想做什么。

And lastly, I read that AFNetworking, is doing a great job in loading url async to urlImage, Which this is what I want to do.

那么AFNetworking更好地完成这项任务吗? (然后是Alamofire)
,如果没有,我不明白如何将AFNetworking添加到我的迅速项目中,除了添加#import AFNetworking.h外,我还需要添加哪些文件?

So is AFNetworking is better for this task? (then Alamofire) and if not, I did not understand how to add AFNetworking to my swift project, beside adding the #import "AFNetworking.h" which files do I need to add?

请说明哪种方法最好,我的需求是性能,指责和缓存。
视图收集控制器的行为类似于Instegram,并在向下滚动时加载图像。
我希望我对自己的需求已经足够清楚了,
谢谢你

please explain which method is the best, my needs are performance, accusation, caching. the view collection controller acts like Instegram and is loading images, while scrolling down. I hope I was clear enough about what I need, thank you

推荐答案

正如作者在Alamofire README.md

As authors mention in Alamofire README.md:


何时应使用AFNetworking?




  • UIKit扩展,例如异步将图像加载到UIImageView

所以回答您的问题:


那么AFNetworking是否更适合此任务?

So is AFNetworking is better for this task?

是!

但是,如果仍然想使用香草 Alamofire 项目,则可以通过以下方式获取图像:

But if still want to use vanilla Alamofire project, you can fetch image this way:

   Alamofire.request(.GET, "https://robohash.org/123.png").response { (request, response, data, error) in
        self.myImageView.image = UIImage(data: data, scale:1)
    }






PS


P.S.

但是,如果您只是想加载图像(当然是异步的),则无需使用 Alamofire AFNetworking
只需在代码中添加此小扩展名即可:

But if you just want to load image (of course async) - you don't need to use Alamofire or AFNetworking at all. Just add this small extension in your code:

extension UIImageView {
    public func imageFromUrl(urlString: String) {
        if let url = NSURL(string: urlString) {
            let request = NSURLRequest(URL: url)
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
                (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
                self.image = UIImage(data: data)
            }
        }
    }
}

并使用它:

myImageView.imageFromUrl("https://robohash.org/123.png")

这篇关于如何使用Alamofire快速加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆