Unzip NSData folder from URL Swift -
the code below works downloading single image url.
func imageforimageurlstring(imageurlstring: string, completion: (image: uiimage?, success: bool) -> void) { guard let url = nsurl(string: imageurlstring), let data = nsdata(contentsofurl: url), let image = uiimage(data: data) else { completion(image: uiimage(named:"absolut5.png"), success: false); return } completion(image: image, success: true) }
however of urls working zipped folder several png files inside. have adjusted above code bring data. seems work
func datafordataurlstring(imageurlstring: string, completion: (data: nsdata?, success: bool) -> void) { guard let url = nsurl(string: imageurlstring), let data = nsdata(contentsofurl: url) else { return } completion(data: data, success: true) }
usage
let imageurlstring = item.valueforkey(url) as! string self.datafordataurlstring(imageurlstring, completion: { (data, success) -> void in if success { guard let datadocs = data else { return } // error handling here documentdata = datadocs } else { // error handling here. } })
but have not found unzip/loop through contents of returned data. not in json format, raw data strings of numbers when printed.
any appreciated (it worth mentioning doing in background nsoperationqueue completion block saving result in core data. have looked @ few nsurlsessiondownloadtask options, these seem go onto different thread, , still don't work)
just post solution went
func unzipdata(objectdata: nsmanagedobject) { var paths = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.userdomainmask, true) let documentsdir = paths[0] let zippath = documentsdir.stringbyappendingstring("myzipfiles") let folderpath = documentsdir.stringbyappendingstring("/doclibfiles") // folder name in document directory var optdata = nsdata(data: objectdata.valueforkey("image") as! nsdata) print(objectdata.valueforkey("imageurl") as! string) optdata.writetofile(zippath, atomically: true) let success = filemanager.fileexistsatpath(zippath) bool if success == false { { try! filemanager.createdirectoryatpath(folderpath, withintermediatedirectories: true, attributes: nil) } } queue.addoperationwithblock { () -> void in let operation1 = nsblockoperation(block: { let unzipped = ssziparchive.unzipfileatpath(zippath, todestination: folderpath) }) operation1.completionblock = { if queue.operationcount == 0 { dispatch_async(dispatch_get_main_queue(), { if queue.operationcount == 0 { self.retrievefiles() } }) } } queue.addoperation(operation1) } } func getdocumentsurl() -> nsurl { let documentsurl = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask)[0] return documentsurl } func fileindocumentsdirectory(filename: string) -> string { let fileurl = getdocumentsurl().urlbyappendingpathcomponent(filename) return fileurl.path! } func retrievefiles() { var paths = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.userdomainmask, true) let documentsdir = paths[0] let zippath = documentsdir.stringbyappendingstring("myzipfiles") let folderpath = documentsdir.stringbyappendingstring("/doclibfiles") // folder name in document directory { let filelist = try filemanager.contentsofdirectoryatpath(folderpath) print(filelist) print("filename") filename in filelist { filenamearray.append(filename) } } catch let error nserror { print("could not save \(error)") } { item in filenamearray { print("item \(item)") let imagepath = fileindocumentsdirectory("doclibfiles/\(item)") imagearray.append(uiimage(contentsoffile: imagepath)!) } print("filename array \(filenamearray)") print("image array \(imagearray)") unzipdelegate!.unzipset(imagearray) } }
Comments
Post a Comment