【问题标题】:NSFileManager fileExistsAtPath returns true and false for the same path in two different classes after a segue (and the file exists)NSFileManager fileExistsAtPath 在 segue 之后为两个不同类中的同一路径返回 true 和 false(并且文件存在)
【发布时间】:2016-09-16 14:00:14
【问题描述】:

我要疯了。我有一个类,它列出相册中的图片并为每个图片检索一个 NSURL,例如

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

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

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

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...]
            }
        }
    }
}

但一旦到了那里,

NSFileManager.defaultManager().fileExistsAtPath(thePath!)

只是不断返回错误。我尝试了很多调整,比如将路径作为字符串传递,使用 NSURL 对象的“路径”和“相对路径”属性,我可以在网上找到的所有建议,没有任何作用,找不到文件,以下总是返回 nil

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

控制台输出:

第一次访问 URL 并存储时:

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

当我再次尝试访问它时

(lldb) print NSFileManager.defaultManager().fileExistsAtPath(clickedPicture.path!.path!)
(Bool) $R4 = false
(lldb) print (clickedPicture.path!.path!)
(String) $R5 = "/var/mobile/Media/DCIM/100APPLE/IMG_0045.JPG"

【问题讨论】:

  • 你有没有想过这个问题?我也有同样的问题。

标签: ios swift


【解决方案1】:

为了回答 Raymond26 和其他可能有类似问题的人,我终于明白,你永远不应该对应用沙箱之外的文件路径做出任何假设,例如“/var/mobile/Media/DCIM/100APPLE/IMG_0045.JPG "

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

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)")
            }

        })
    }
}

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多