iOS Development Swift version asynchronously loads network pictures (with cache and default Picture)
Source: Internet
Author: User
<span id="Label3"></p>The swift version of iOS development asynchronously loads network pictures<p><p><span style="font-size: 16px;">With the Sdwebimage asynchronous load network picture function is similar, just the code is relatively simple, the function is not perfect and powerful sd, support default add picture, support local Cache.</span><br></p></p> <p><p><span style="font-size: 16px;">the core code for loading images asynchronously is as Follows:</span><br></p></p> <pre class="brush:shell;toolbar: true; auto-links: false;"> func setzyhwebimage (url:nsstring?, defaultimage:nsstring?, iscache:bool) { var ZYHImage:UIImage? if url == nil { return } // Set default picture if defaultImage != nil { self.image=uiimage (named: defaultImage!) } //whether to cache processing if isCache { //cache Management class var data: Nsdata?=zyhwebimagechchecenter.readcachEfromurl (url!) if data != nil { zyhimage= UIImage (data: data!) self.image= zyhimage }else{ //getting Asynchronous Threads var dispath=dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0) Dispatch_async (dispath, { () -> Void in var url:nsurl&nbsP;= nsurl (string: url!)! var data:nsdata?=nsdata (contentsofurl: url) if data != nil { zyhimage=uiimage (data: data!) //write Cache Zyhwebimagechchecenter.writecachetourl (url!, data: data!) &nbSp; //refresh ui in main thread dispatch_async ( Dispatch_get_main_queue (), { () -> Void in //refresh Main 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!) //write Cache dispatch_async (dispatch_get_main_queue (), { () -> Void in //refresh Main ui self.image=ZYHImage }) } }) } } }</pre> <p><p> <span style="font-size:16px;"> Cache processing Here is the way to write files, through the file name to manage the cache, the framework is not perfect, followed by the addition of cache cleanup Functions. The core code for the cache is as follows: </span> <br> </p></p> <pre class="brush:shell;toolbar: true; auto-links: false;">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)) } //set Cache Path 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) } //for string Processing var newurl: nsstring &Nbsp; 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 }</pre> <p><p><span style="font-size: 16px;"></span><span style="font-size: 16px;">Frame of GitHub address, welcome correction and Extension: Https://github.com/ZYHshao/swift-ZYHWebImage</span></p></p> <p><p><span style="font-size: 16px;">Because of the different versions of xcode, The Swift language syntax varies from time to time, and this version is available in 6.1, and a few changes are required in later Versions.</span><br></p></p> <p><p><span style="font-size: 16px;"></span><br></p></p> <p><p><br></p></p><p><p>iOS Development Swift version asynchronously loads network pictures (with cache and default Picture)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service