在segue(并且文件存在)之后,NSFileManager fileExistsAtPath对两个不同类中的相同路径返回true和false

时间:2016-09-16 14:00:14

标签: ios swift

我为此疯狂。我有一个课程,列出相册中的图片并为每个图片检索NSURL,例如

ng-option="p in publi track by $index | exclude: pSelect :$index"

我在那里放了一个断点并检查,文件存在都很好。

我存储这些URL。在用户操作上,我调用另一个类来显示其中一个图像,并将segue上的该URL传递给新视图控制器。

"/var/mobile/Media/DCIM/100APPLE/IMG_0045.JPG"

但是一旦那里,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "picDetailsSegue" {
        if let picReviewController = segue.destinationViewController as? PicReviewViewController
        {
            if(clickedPicture != nil)
            {
                picReviewController.existingFilePath = clickedPicture.urlPath.path!
                [initialize other stuff...]
            }
        }
    }
}

只是不断返回虚假。我尝试了许多调整,比如将路径作为字符串传递,使用" path"和#34; relativePath" NSURL对象的属性,我在网上找到的所有建议,无效,找不到文件,以下内容总是返回

NSFileManager.defaultManager().fileExistsAtPath(thePath!)

控制台输出:

我第一次访问网址并存储它:

let image = UIImage(contentsOfFile: theURL.path!)

当我尝试再次访问时

(lldb) print NSFileManager.defaultManager().fileExistsAtPath(url!.path!)
(Bool) $R1 = true
(lldb) print url!.path
(String?) $R3 = "/var/mobile/Media/DCIM/100APPLE/IMG_0045.JPG"

1 个答案:

答案 0 :(得分:0)

要回答Raymond26和其他可能有类似问题的人,我终于明白你不应该在应用程序沙箱外的文件路径上做任何假设,例如“/var/mobile/Media/DCIM/100APPLE/IMG_0045.JPG “

相反,您应该使用PHAsset API来读取和写入这些文件,如果您确实需要文件路径(就像我在处理文件中的C库时那样),您应该将文件复制到您的文件中app之前的临时目录。

static func copyToTemp(asset: PHAsset, desFileName: NSURL, requestedMaxSize: Int) throws
{
    NSLog("copying asset to thumb for editing to: \(desFileName.path) with max size \(requestedMaxSize)")

    // Clean temp as iOS doesnt do much overwriting in general
    if(NSFileManager.defaultManager().fileExistsAtPath(desFileName.path!))
    {
        try NSFileManager.defaultManager().removeItemAtURL(desFileName)
    }

    if(requestedMaxSize < 0) // No resizing, just copy
    {
        let options = PHImageRequestOptions()
        options.synchronous = true
        PHImageManager.defaultManager().requestImageDataForAsset(asset, options: options, resultHandler: { (data: NSData?, name: String?, orientation: UIImageOrientation, stuff: [NSObject: AnyObject]?) in
            do
            {
                try data?.writeToURL(desFileName, options: NSDataWritingOptions.AtomicWrite)
            }
            catch
            {
                NSLog("error writing full-sized to temp: \(error)")
            }
        })
    }
    else // Copy and resize
    {
        let size = CGSizeMake(CGFloat(requestedMaxSize), CGFloat(requestedMaxSize))
        let options = PHImageRequestOptions()
        options.synchronous = true
        options.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat
        options.resizeMode = PHImageRequestOptionsResizeMode.Exact
        PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: size, contentMode: PHImageContentMode.AspectFit, options: options, resultHandler:
        {
            (image: UIImage?, obj: [NSObject: AnyObject]?) -> Void in
            do
            {
                try UIImageJPEGRepresentation(image!, 0.8)!.writeToURL(desFileName, options: NSDataWritingOptions.AtomicWrite)
            }
            catch
            {
                NSLog("error writing resized to temp: \(error)")
            }

        })
    }
}