【问题标题】:How to integrate latest SDWebImage API in my Swift based project?如何在基于 Swift 的项目中集成最新的 SDWebImage API?
【发布时间】:2014-11-10 11:11:17
【问题描述】:

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

#ifndef MyProject_Bridging_Header_h
#define MyProject_Bridging_Header_h

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

#endif

我也添加了框架并将 SDWebImage 项目拖到我的应用程序中,如 here 解释的那样

我真的在这方面苦苦挣扎。请帮忙!作为参考,我添加了一张显示错误的图片!

【问题讨论】:

  • 你最好使用 Haneke 在 swift 中缓存图像github.com/Haneke/HanekeSwift
  • @Saurabh Prajapati:你用过这个吗?怎么样?
  • 是的!几乎在 2 个月前!
  • @SaurabhPrajapati 我们一直在使用 Haneke,直到我们发现它在加载图像时会定期使应用程序崩溃。我们在 Github 上提出了一个问题,但几天没有得到回应。现在切换到 SDWebImage。

标签: swift xcode6 sdwebimage ios8.1


【解决方案1】:

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

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"

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

【讨论】:

  • 我可以使用您的解决方案,但没有标题,它仍然可以正常工作。谢谢。
【解决方案2】:

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

制作一个 Obj C Bridging:File -> New -> Source -> Header File -> Name as 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
    }

【讨论】:

    【解决方案3】:

    swift 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      相关资源
      最近更新 更多