NSFileManager.defaultManager()。removeItemAtPath仅在应用终止后删除文件

时间:2015-12-07 22:51:13

标签: swift nsfilemanager

所以我使用下面的方法处理我的应用程序的获取,删除和保存图像。当我调用removeImage它成功时,当我使用NSFileManager.defaultManager().fileExistsAtPath时,它表示该文件不存在。但是,当我调用getImage时,即使已删除图像,它仍然可以抓取图像。当我完全使用该应用并重新启动时getImage按预期工作,直到我使用removeImage。我还尝试在removeImage中包裹dispatch_async(dispatch_get_main_queue(),{})来电,但仍然无效。

我的代码:

class func removeImage(user:User){
    let context = getManagedObjectContext()
    context.performBlockAndWait({
        guard let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as [String]! where paths.count > 0 else{
            return
        }

        do{
            if let dirPath = paths[0] as String!{
                let path = "\(user.id)_\(user.name).png"
                let readPath = (dirPath as NSString).stringByAppendingPathComponent(path)
                try NSFileManager.defaultManager().removeItemAtPath(readPath)
            }
        }catch{
        }
    })
}

class func saveImage(user:User,image:UIImage){
    let path = "\(user.id)_\(user.name).png"
    guard let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as [String]! where paths.count > 0 else{
        return
    }
    if let dirPath = paths[0] as String!{
        let writePath = (dirPath as NSString).stringByAppendingPathComponent(path)
        UIImagePNGRepresentation(image)!.writeToFile(writePath, atomically: true)
    }
}


class func getImage(user:User)->UIImage?{
    let path = "\(user.id)_\(user.name).png"
    guard let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as [String]! where paths.count > 0 else{
        return nil
    }
    if let dirPath = paths[0] as String!{
        let readPath = (dirPath as NSString).stringByAppendingPathComponent(path)
        return UIImage(named: readPath)
    }
    return nil
}

UPDATE / FIX!将代码更改为此并且有效:

class func removeImage(user:User){
    let manager = NSFileManager.defaultManager()
    do{
        let directoryURL = try manager.URLForDirectory(.DocumentationDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
        if #available(iOS 9.0, *) {
            let url = NSURL(fileURLWithPath: "\(user.id)_\(user.name).png", relativeToURL: directoryURL)
            try manager.removeItemAtURL(url)
        } else {
            let url = directoryURL.URLByAppendingPathComponent("\(user.id)_\(user.name).png")
            try manager.removeItemAtURL(url)
        }
    }catch{

    }
}


class func saveImage(user:User,image:UIImage){
    let manager = NSFileManager.defaultManager()
    do{
        let directoryURL = try manager.URLForDirectory(.DocumentationDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
        if #available(iOS 9.0, *) {
            let url = NSURL(fileURLWithPath: "\(user.id)_\(user.name).png", relativeToURL: directoryURL)
            UIImagePNGRepresentation(image)!.writeToURL(url, atomically: true)
        } else {
            let url = directoryURL.URLByAppendingPathComponent("\(user.id)_\(user.name).png")
            UIImagePNGRepresentation(image)!.writeToURL(url, atomically: true)
        }
    }catch{

    }
}

class func getImage(user:User)->UIImage?{
    let manager = NSFileManager.defaultManager()
    do{
        let directoryURL = try manager.URLForDirectory(.DocumentationDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
        if #available(iOS 9.0, *) {
            let url = NSURL(fileURLWithPath: "\(user.id)_\(user.name).png", relativeToURL: directoryURL)
            if let data = NSData(contentsOfURL: url){
                return UIImage(data: data)
            }
        } else {
            let url = directoryURL.URLByAppendingPathComponent("\(user.id)_\(user.name).png")
            if let data = NSData(contentsOfURL: url){
                return UIImage(data: data)
            }
        }
    }catch{

    }
    return nil
}

0 个答案:

没有答案