【问题标题】:Using the Local Data Store in Parse and Swift在 Parse 和 Swift 中使用本地数据存储
【发布时间】:2015-09-19 20:13:48
【问题描述】:

我有一个UIImagePickerController,它允许用户将图像添加到 localDatastore:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    print("photo picked!") 
    //self.collectionView?.reloadData()
    //try writing to parse local datastore?

    let imageObject = PFObject(className: "imagePicked")        
    imageObject["theImg"] = PFFile(data: UIImageJPEGRepresentation(pickedImage, 0.1)!)
    imageObject["id"] = "123321"
    imageObject.pinInBackgroundWithName("imgs") { (success, error) -> Void in
        if error == nil{
            print("Object pinned!")         
            self.collectionView?.reloadData()
        } else {
            print(error)
        }
    }

    self.dismissViewControllerAnimated(true) { () -> Void in
        self.collectionView?.reloadData()
    }
}

然后在 viewDidLoad 中,我将 localDatastore 中的所有对象附加到一个数组中 -

let query = PFQuery(className: "imagePicked")
    query.fromLocalDatastore()
    query.fromPinWithName("imgs")
    // Put all of the objects found in an array
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {              
            let objects = objects as! AnyObject
            for object in objects as! [AnyObject] {
                let pictureData = object["theImg"] as! PFFile
                pictureData.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
                    if error == nil {           
                        if let imageData = UIImage(data: imageData!) where error == nil {
                            self.theImages.append(imageData)             
                            self.collectionView?.reloadData()
                            print("The images were added!")
                        }
                    } else {
                        print(error)
                    }
                }) 
            }
        } else {
            print(error)
        }
    }
}

最后,在 UICollectionView I am then populating theUICollectionViewCell` 的 dataSource 方法中使用 Parse 中的图像数组。

 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = self.collectionView?.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! theCell
    //add the images from the array
    cell.theImage.image = theImages[indexPath.row]
    return cell
}

问题是我收到了这个错误:

2015-09-19 15:49:35.216 SecureSearchv3[17816:700076] [错误]:捕获“NSInvalidArgumentException”,原因为“*** -[_NSPlaceholderData initWithContentsOfFile:options:error:]: nil file argument":

【问题讨论】:

  • 你找到解决这个问题的方法了吗?
  • 是的,我找到了答案
  • 你能把它作为答案发布在下面吗?我也遇到了同样的问题
  • 我会,但是我现在在学校
  • 我也有。原因是什么?

标签: ios swift parse-platform swift2


【解决方案1】:

我发现了同样的问题。有办法解决它(我知道这是一篇旧帖子......)

LocalDataStore 不会存储 UIImage 所存储的 PFiles。要绕过它,您需要将图像存储在文档目录中:

SWIFT 3

let imageData = UIImagePNGRepresentation(image)!  
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)  
let imageURL = docDir.appendingPathComponent("tmp.png")  
try! imageData.write(to: imageURL)  

然后去检索:

let newImage = UIImage(contentsOfFile: imageURL.path)!

这不会处理任何错误.....

使用您的 PFObject,您将保存在本地数据存储存储“tmp.png”或您调用它的任何路径中,因此很容易回调。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-06
    • 2015-12-02
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2015-08-12
    相关资源
    最近更新 更多