UIDevice.currentDevice()。模型不再作为模拟器返回

时间:2015-10-22 14:54:48

标签: uiimage ios-simulator swift2 xcode7

我有一个我正在应用程序中生成的图像,并尝试保存在模拟器中以便在生成后进行检查。 在我的应用程序的早期版本中,要在模拟器上运行时将图像保存到我的计算机,我将使用以下代码:

    let img = self.getImageFromDrawingView()
    UIImageWriteToSavedPhotosAlbum(img, self, "image:didFinishSavingWithError:contextInfo:", nil)

    let string = UIDevice.currentDevice().model
    if ( string as NSString).containsString("Simulator")
    {

        let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL!

        let fileURL = documentsDirectoryURL.URLByAppendingPathComponent("image" + savedImageCounter.description + ".jpg")
        NSLog("Doc URL is " + documentsDirectoryURL.description)
        UIImageJPEGRepresentation(img, 1.0)!.writeToURL(fileURL, atomically: true)
    }

以下一行:

let string = UIDevice.currentDevice().model

用于返回“iPad模拟器”,因此我可以将照片保存到计算机上的已知位置。但是,现在此调用只返回“iPad”或“iPhone”,模拟器部分不再存在。 现在我无法知道模拟器在哪里保存图像,我如何知道图像在本地保存在哪里?

会喜欢任何输入。

1 个答案:

答案 0 :(得分:0)

不要在运行时执行此类操作。你没有理由在运行时做你在构建时可以做的事情。使用TargetConditionals.h并执行以下操作:

#if TARGET_OS_SIMULATOR
    NSLog(@"This is a simualtor.");
#else
    NSLog(@"This is a device.");
#endif

至于确定容器在磁盘上的位置,只需运行:

xcrun simctl get_app_container booted com.mycompany.myapp