无法打开obj文件

时间:2018-02-19 03:20:20

标签: swift scenekit arkit

所以我正在开发一个项目来拉取包含.obj文件的zip文件,解压缩它们并使用ARkit显示它们。目前,我已经能够将文件解压缩并将文件保存在我的文件目录中作为.obj文件,它说该文件存在,但当我尝试将其转换为scn时,它说"不能打开OBJ文件"。这是我的代码,我试图找出出错的地方。文件大小为314 KB,因此我知道这不是问题而且obj文件没有损坏,因为我可以将其下载到我的计算机并打开它。

class ViewController: UIViewController {

var testData: Store?
var logoImage: UIImage?
var urlForObject: String?
var tempDirectoryString: String?
var tempDirectoryURL: URL?
var testModelData: Data?
var testData2: Data?
var testData3: Models?
var tempDocumentsURL: URL?


@IBOutlet weak var logoImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.clearCowZipFolder()
    self.clearCowFolder()

    self.requestZipFile(){ response in
        self.testData2 = response
        do{
            let json = try? JSONSerialization.jsonObject(with: response, options: [])

            if let array = json as? [String] {
                if let firstObject = array.first {
                    //print(firstObject)
                }

                for object in array {
                    // access all objects in array
                }

                for case let string as String in array {
                    // access only string values in array
                }
            }

        }catch{
            print("errorrrr")
        }
    }
    self.tempDocumentsURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
    let destinationFileUrl = self.tempDocumentsURL!.appendingPathComponent("Man.zip")
    var destPath = self.tempDocumentsURL!.appendingPathComponent("Man")

    //Create URL to the source file you want to download
    let fileURL = URL(string: "http://markitapi.com/stores/TestStore/models/Man")

    let sessionConfig = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfig)

    let request = URLRequest(url:fileURL!)

    let task = session.downloadTask(with: request) { (tempLocalUrl, response3, error) in
        if let tempLocalUrl = tempLocalUrl, error == nil {
            // Success
            if let statusCode = (response3 as? HTTPURLResponse)?.statusCode {
                print("Successfully downloaded. Status code: \(statusCode)")

                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                } catch (let writeError) {
                    print("Error creating a file \(destinationFileUrl) : \(writeError)")
                }

                do{
                    let unzipDirectory = try Zip.quickUnzipFile(destinationFileUrl) //Unzip
                        print("HHERHERHEHRE")
                        destPath = unzipDirectory
                }catch{
                    print("error while unzipping")
                }

                print("UNZIPPED PATH")
                //print(destPath)

                let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
                let url = NSURL(fileURLWithPath: path)
                if let pathComponent = url.appendingPathComponent("Man/Man.obj") {
                    let filePath = pathComponent.path
                    //print(filePath)
                    let fileManager = FileManager.default
                    if fileManager.fileExists(atPath: filePath) {
                        print("FILE AVAILABLE")
                    } else {
                        print("FILE NOT AVAILABLE")
                    }
                } else {
                    print("FILE PATH NOT AVAILABLE")
                }

                let url2 = (String(describing: destPath) + "Man.obj")
                let url3 = URL(fileURLWithPath: url2)
                print(url3.pathExtension)
                let asset = MDLAsset(url: url3)
                //print(asset)
                let object = asset.object(at: 0)
                let node = SCNNode(mdlObject: object)
                //print(node)

            }

        } else {
            //print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
        }
    }
    task.resume()

}

func clearCowZipFolder() {
    let fileNameToDelete = "Man.zip"
    var filePath = ""

    // Fine documents directory on device
    let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)

    if dirs.count > 0 {
        let dir = dirs[0] //documents directory
        filePath = dir.appendingFormat("/" + fileNameToDelete)
        //print("Local path = \(filePath)")

    } else {
        print("Could not find local directory to store file")
        return
    }


    do {
        let fileManager = FileManager.default

        // Check if file exists
        if fileManager.fileExists(atPath: filePath) {
            // Delete file
            try fileManager.removeItem(atPath: filePath)
        } else {
            print("File does not exist")
        }

    }
    catch let error as NSError {
        print("An error took place: \(error)")
    }
}

func clearCowFolder() {
    let fileNameToDelete = "Man"
    var filePath = ""

    // Fine documents directory on device
    let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)

    if dirs.count > 0 {
        let dir = dirs[0] //documents directory
        filePath = dir.appendingFormat("/" + fileNameToDelete)
        //print("Local path = \(filePath)")

    } else {
        print("Could not find local directory to store file")
        return
    }


    do {
        let fileManager = FileManager.default

        // Check if file exists
        if fileManager.fileExists(atPath: filePath) {
            // Delete file
            try fileManager.removeItem(atPath: filePath)
        } else {
            print("File does not exist")
        }

    }
    catch let error as NSError {
        print("An error took place: \(error)")
    }
}

func requestZipFile(success successBlock: @escaping (Data) -> Void){
    Alamofire.request("http://markitapi.com/stores/TestStore/models").responseJSON { (response) in
        do{
            if(response.result.isSuccess){
                successBlock(response.data!)
            }
        }catch{
            print("error")
        }
        }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

代码的第一部分检查文件是否可用并且它确实打印了该语句,所以我知道该文件在那里。

0 个答案:

没有答案