11

我已经将 SDWebImage 与 Objective C 一起使用,它对我来说非常有用,但现在我正在学习 Swift 并尝试集成最新版本的 API,但我坚持每一步,因为 API 在 Objective C 中,并且没有提到要使用的步骤使用 Swift 的 API。我阅读了文档并创建了桥头文件并包含了所需的文件,如下所示:

#ifndef MyProject_Bridging_Header_h
#define MyProject_Bridging_Header_h

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

#endif

我也添加了框架并将 SDWebImage 项目拖到我的应用程序中,如此处所述

我真的在这方面苦苦挣扎。请帮忙!作为参考,我添加了显示错误的图像!在此处输入图像描述

4

3 回答 3

24

这是一个应该可以工作的代码示例:

let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in
    println(self)
}

let url = NSURL(string: "http://placehold.it/350x150")

self.imageView.sd_setImageWithURL(url, completed: block)

并在您的桥接头文件中:

#import "UIImageView+WebCache.h"

所以你的桥接头文件应该可以工作,但有时我遇到了桥接头问题,在这些情况下,我只是删除它,然后再次添加它,之后一切正常。

于 2014-12-23T09:56:02.090 回答
3

最好的选择是将 SDWebImage 文件夹拖放到项目中。确保勾选“如果需要,复制项目”。

制作一个 Obj C 桥接:文件 -> 新建 -> 源 -> 头文件 -> 名称为 AppName-Bridging-Header。

添加以下内容:

        #ifndef AppName_AppName_Bridging_Header_h
        #define AppName_AppName_Bridging_Header_h

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

    or

 #import "UIImageView+WebCache.h"   

参考:https ://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

注意:构建设置,在 Swift 编译器 - 代码生成中,确保下的 Objective-C Bridging Header 构建设置具有桥接头文件的路径。- 类似于 testSD/testSD-Bridging-Header.h 或 testSD-Bridging-Header.h(打开项目文件夹并找到头文件路径)

现在尝试使用以下代码:

let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in
    println(self)
}

let url = NSURL(string: "http://arrow_upward.com/350x150")
self.imageView.sd_setImageWithURL(url, completed: block)

假设如果您使用 UICollectionView 来填充缓存图像,请尝试使用此代码。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = photoListCollectionView.dequeueReusableCellWithReuseIdentifier("scoutimagecellidentifier", forIndexPath: indexPath) as! ScoutImageCell

        //Loading image from server using SDWebImage library
        let thumbImageUrl  =   NSURL(string: self.photoPropertyArray[indexPath.row] as String)

        //Image Fetching is done in background GCD thread        

SDWebImageManager.sharedManager().downloadImageWithURL(thumbImageUrl, options: [],progress: nil, completed: {[weak self] (image, error, cached, finished, url) in

if let wSelf = self {

                //On Main Thread
                dispatch_async(dispatch_get_main_queue()){
                    cell.scoutimage.image = image
                    cell.photoloader.stopAnimating()
                }
            }
            })
        return cell
    }
于 2015-10-26T10:36:56.903 回答
1

迅捷3.0代码

导入 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)
于 2017-05-15T06:52:10.633 回答