iOS開發swift版非同步載入網狀圖片(帶緩衝和預設圖片)

來源:互聯網
上載者:User

標籤:

iOS開發之swift版非同步載入網狀圖片

    與SDWebImage非同步載入網狀圖片的功能相似,只是代碼比較簡單,功能沒有SD的完善與強大,支援預設添加圖片,支援本機快取。

     非同步載入圖片的核心代碼如下:

 func setZYHWebImage(url:NSString?, defaultImage:NSString?, isCache:Bool){        var ZYHImage:UIImage?        if url == nil {            return        }        //設定預設圖片        if defaultImage != nil {            self.image=UIImage(named: defaultImage!)        }        //是否進行緩衝處理        if isCache {        //緩衝管理類            var data:NSData?=ZYHWebImageChcheCenter.readCacheFromUrl(url!)            if data != nil {                ZYHImage=UIImage(data: data!)                self.image=ZYHImage            }else{            //擷取非同步線程               var dispath=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)                dispatch_async(dispath, { () -> Void in                    var URL:NSURL = NSURL(string: url!)!                    var data:NSData?=NSData(contentsOfURL: URL)                    if data != nil {                        ZYHImage=UIImage(data: data!)                        //寫緩衝                        ZYHWebImageChcheCenter.writeCacheToUrl(url!, data: data!)                        //主線程中重新整理UI                        dispatch_async(dispatch_get_main_queue(), { () -> Void in                            //重新整理主UI                            self.image=ZYHImage                        })                    }                                    })            }        }else{            var dispath=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)            dispatch_async(dispath, { () -> Void in                var URL:NSURL = NSURL(string: url!)!                var data:NSData?=NSData(contentsOfURL: URL)                if data != nil {                    ZYHImage=UIImage(data: data!)                    //寫緩衝                    dispatch_async(dispatch_get_main_queue(), { () -> Void in                        //重新整理主UI                        self.image=ZYHImage                    })                }                            })        }    }    }

緩衝的處理這裡採用的是寫檔案的方式,通過檔案名稱來對緩衝進行管理,這個架構還不完善,後面會加入緩衝清除等功能。緩衝的核心代碼如下:

class func readCacheFromUrl(url:NSString)->NSData?{        var data:NSData?        var path:NSString=ZYHWebImageChcheCenter.getFullCachePathFromUrl(url)        if NSFileManager.defaultManager().fileExistsAtPath(path) {            data=NSData.dataWithContentsOfMappedFile(path) as? NSData        }        return data    }        class func writeCacheToUrl(url:NSString, data:NSData){        var path:NSString=ZYHWebImageChcheCenter.getFullCachePathFromUrl(url)       println(data.writeToFile(path, atomically: true))    }    //設定緩衝路徑    class func getFullCachePathFromUrl(url:NSString)->NSString{        var chchePath=NSHomeDirectory().stringByAppendingString("/Library/Caches/MyCache")        var fileManager:NSFileManager=NSFileManager.defaultManager()        fileManager.fileExistsAtPath(chchePath)        if !(fileManager.fileExistsAtPath(chchePath)) {            fileManager.createDirectoryAtPath(chchePath, withIntermediateDirectories: true, attributes: nil, error: nil)        }        //進行字串處理        var newURL:NSString        newURL=ZYHWebImageChcheCenter.stringToZYHString(url)        chchePath=chchePath.stringByAppendingFormat("/%@", newURL)        return chchePath    }        class func stringToZYHString(str:NSString)->NSString{        var newStr:NSMutableString=NSMutableString()        for var i:NSInteger=0; i < str.length; i++ {            var c:unichar=str.characterAtIndex(i)            if (c>=48&&c<=57)||(c>=65&&c<=90)||(c>=97&&c<=122){                newStr.appendFormat("%c", c)            }        }        return newStr.copy() as NSString            }

架構的github地址,歡迎指正與擴充:https://github.com/ZYHshao/swift-ZYHWebImage

因xcode的版本不同,swift語言文法隨環境時常會變化,此版本在6.1中可用,更高版本中需要修改少部分即可。



iOS開發swift版非同步載入網狀圖片(帶緩衝和預設圖片)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.