简体   繁体   English

如何在 swift xcode 6.3.1 中导入和使用 SDWebImage

[英]How to import and use SDWebImage in swift xcode 6.3.1

Im new to Swift and now making a project that includes showing many photos from the web and I understand that I need to use SDWebImage .我是Swift新手,现在正在制作一个项目,其中包括展示来自网络的许多照片,我知道我需要使用SDWebImage I saw related questions here and in other places but all are in Objective-C syntax and not working for me.我在这里和其他地方看到了相关问题,但都是Objective-C语法,对我不起作用。

What I did till now:到目前为止我所做的:

  1. I downloaded the zip from GitHub我从 GitHub 下载了 zip
  2. Copied SDWebImage folder to my folderSDWebImage文件夹复制到我的文件夹
  3. Tried all possible combinations for import尝试了所有可能的import组合

#import <"SDWebImage/UIImageView+WebCache.h">

import SDWebImage/UIImageView+WebCache.h

etc..等等..

Can someone please help me import it有人可以帮我导入吗

Firstly, you need configure Bridging Header , it is described here at SO .首先,您需要配置Bridging Header在 SO 中对此进行了描述。 Use the following:使用以下内容:

#import <SDWebImage/UIImageView+WebCache.h>

This will import Objective-C code to Swift code这会将Objective-C代码导入Swift代码

Secondly, just use it, like:其次,只需使用它,例如:

self.imageView.sd_setImageWithURL(self.imageURL)

As far as I understood you already have a Bridging header and only need to organize your imports.据我了解,您已经有一个桥接头,只需要组织您的导入。 Since you copied the source files directly into your project without using cocoapods or Carthage you do the import like this:由于您不使用 cocoapods 或 Carthage 将源文件直接复制到您的项目中,因此您可以像这样进行导入:

#import "UIImageView+WebCache.h"

In swift 5.1 simply import the SdWebimage and use extenion在 swift 5.1 中,只需导入 SdWebimage 并使用扩展名

extension UIImageView{扩展 UIImageView{

func downloadImage(url : String, placeHolder : UIImage?) throws{
    if let url = URL(string: url){
        self.sd_imageIndicator?.startAnimatingIndicator()
        self.sd_setImage(with: url) { (image, err, type, url) in

            if err != nil{
                    self.sd_imageIndicator?.stopAnimatingIndicator()
                    print("Failed to download image")
                }
                self.sd_imageIndicator?.stopAnimatingIndicator()
            }
        }

    }
}

The only way it worked for me (using mac os 10.11.4 and ios 9.3) was this:它对我有用的唯一方法(使用 mac os 10.11.4 和 ios 9.3)是这样的:

  1. download and unpack framework from link: https://github.com/rs/SDWebImage/releases/download/3.6/SDWebImage-3.6.framework.zip :从链接下载和解压框架: https://github.com/rs/SDWebImage/releases/download/3.6/SDWebImage-3.6.framework.zip

  2. drag that framework into x-code project and check option: Copy items if needed (In project navigator you should now see suitcase icon containing a folder with .h files)将该框架拖到 x-code 项目中并选中选项:如果需要,请复制项目(在项目导航器中,您现在应该看到包含带有 .h 文件的文件夹的手提箱图标)

  3. create new bridging header file: cmd+n -> file -> new -> file... and select "header" template icon创建新的桥接头文件:cmd+n -> file -> new -> file... 并选择“header”模板图标
  4. open that file, write line (just above #endif:) #import < SDWebImage/UIImageView+WebCache.h > (this is path of one of header files from framework)打开该文件,写一行(就在#endif 之上:)#import < SDWebImage/UIImageView+WebCache.h > (这是框架中的头文件之一的路径)

  5. the most important step from @Azat answer above, is to connect the file you just made with project, in build settings:上面@Azat 回答中最重要的一步是在构建设置中将您刚刚制作的文件与项目连接起来:

    • select project name (blue icon in Project navigator)选择项目名称(项目导航器中的蓝色图标)
    • select "Build Settings" on your right start to type "Bridging header..." and when you have row containing "Objective-C Bridging header"选择“构建设置”在你的右边开始输入“桥接标题...”,当你有包含“Objective-C 桥接标题”的行时
    • press twice in empty field in that row (window pops-up), and drag bridging header file from Project navigator into that window.在该行的空白字段中按两次(弹出窗口),然后将桥接头文件从项目导航器拖到该窗口中。

      1. hopefully now you can use library methods as: imageView.setImageWithUrl(url, placeholderImage:)希望现在你可以使用库方法: imageView.setImageWithUrl(url, placeholderImage:)

Swift 3.0 code斯威夫特 3.0 代码

import SDWebImage

let url = URL.init(string: "https://vignette3.wikia.nocookie.net/zelda/images/b/b1/Link_%28SSB_3DS_%26_Wii_U%29.png")

  imagelogo.sd_setImage(with: url , placeholderImage: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM