2016-02-01 57 views
0

下面的代碼將圖像文件夾解壓縮到我創建的文件夾中。然後遍歷它並將名稱添加到數組中,然後遍歷該數組並將這些文件名檢索到圖像數組中。在模擬器中工作但不是iPad的文檔文件夾 - Swift 2.1

它可以在模擬器上完美運行,但是當我在iPad上執行操作時,數據是空的,它不會打印任何內容。我只能假設該文件夾在unzip完成之前無法訪問或正在被搜索,但它不應該像我使用帶有完成塊的NSOperationQueue一樣。

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") // My 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) as Bool 
     if success == false { 
      do { 
       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 = { 
        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") // My folder name in document directory 
     do { 
      let filelist = try fileManager.contentsOfDirectoryAtPath(folderPath) 
      print(filelist) 
      print("filename") 
      for filename in filelist { 
       fileNameArray.append(filename) 
      } 
     } catch let error as NSError { 
      print("Could not save \(error)") 
     } 
     do { 
      for 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) 
     } 
    } 
+0

任何舊文件被重用快速瀏覽你的代碼,可能你需要在這一行中的MyZipFiles之前使用斜線:let zipPath = documentsDir.stringByAppendingString(「/ MyZipFiles」) – Allen

+0

歡呼@Allen,這有點奇怪。我以爲有人可能會接受這一點(這一點實際上沒有做任何事情,它不創建文件夾或文件,但解壓縮只適用於我離開它,我認爲它需要一個不同的指針作爲其現有位置) – ThundercatChris

+0

播放字符串路徑可能會導致錯誤,導致拼寫錯誤。可能在NSURL表單中處理路徑,請參考我的答案。 – Allen

回答

0

林不知道是什麼原因最初的問題在那裏,但我的代碼肯定是過於複雜的過程可能路徑。香港專業教育學院排序現在,希望這會幫助別人的SWIFT 2.1

class UnzipDocument { 
let queue = NSOperationQueue() // BACKGROUND THREAD 
    var imageArray = [UIImage]() 
    var fileNameArray = [String]() 
    var fileManager = NSFileManager.defaultManager() 

let compressedFile = NSTemporaryDirectory().stringByAppendingString("MyZipFiles") 
let uncompressedFolder = NSTemporaryDirectory().stringByAppendingString("MyUnzippedFiles") 

func unzipData(objectData: NSManagedObject){ // THIS IS BASICALLY NSDATA FROM CORE DATA FOR ME 
    let optData = NSData(data: objectData.valueForKey("image") as! NSData) 
    let success = fileManager.fileExistsAtPath(uncompressedFolder) as Bool // CREATES THE FOLDER IF IT DOESNT EXIST 
    if success == false { 
     do { 
      try! fileManager.createDirectoryAtPath(uncompressedFolder, withIntermediateDirectories: true, attributes: nil) 
     } 
    } 
    optData.writeToFile(compressedFile, atomically: true) 
    queue.addOperationWithBlock {() -> Void in 
     let operation1 = NSBlockOperation(block: { 
      SSZipArchive.unzipFileAtPath(self.compressedFile, toDestination: self.uncompressedFolder) 
     }) 
     operation1.completionBlock = { 
      if queue.operationCount == 0 { 
       dispatch_async(dispatch_get_main_queue(), { 
        if queue.operationCount == 0 { 
         self.retrieveFiles() 
        } 
       }) 
      } 
     } 
     queue.addOperation(operation1) 
    } 
} 

func retrieveFiles() { 
    do { 
     let filelist = try fileManager.contentsOfDirectoryAtPath(uncompressedFolder) 
     print(filelist) 
     print("filename") 
     for filename in filelist { 
      self.fileNameArray.append(filename) 
     } 
    } catch let error as NSError { 
     print("Could not save \(error)") 
    } 
    do { 
     for item in fileNameArray { 
      print("item \(item)") 
      let imagePath = uncompressedFolder.stringByAppendingString("/\(item)") 
      imageArray.append(UIImage(contentsOfFile: imagePath)!) 
     } 
     print("filename array \(fileNameArray)") 
     print("image array \(imageArray)") 
     unzipDelegate!.unzipSet(imageArray) 
    } catch { 
    } 
} 
} 

,並刪除使用後的臨時文件/文件夾,以便它可以在不其餘

var fileManager = NSFileManager.defaultManager() 
     let compressedFile = NSTemporaryDirectory().stringByAppendingString("MyZipFiles") 
     let uncompressedFolder = NSTemporaryDirectory().stringByAppendingString("MyUnzippedFiles") 
     do { 
      try fileManager.removeItemAtPath(compressedFile) 
       try fileManager.removeItemAtPath(uncompressedFolder) 
     } catch let error as NSError { 
      print("Could not save \(error)") 
     } 
0

在避免錯誤的情況下由一些微不足道的錯別字產生。在NSURL形式



    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) 
    let documentsDir = paths[0] 
    let url = NSURL(fileURLWithPath: documentsDir) 
    url.URLByAppendingPathComponent("MyZipFiles") 
    url.URLByAppendingPathComponent("docLibFiles") 

+0

再次歡呼@Allen,但解壓縮代碼Im使用,使用字符串路徑。我可以用你的代碼創建文件夾,但無論如何都必須切換回去。加上文件夾總是相同的名稱,所以錯別字不應該是一個問題 – ThundercatChris

相關問題