在Parse和Swift中使用本地数据存储

时间:2015-09-19 20:13:49

标签: ios swift parse-platform swift2

我有一个((-1)^n)/(2n + 1),允许用户将图像添加到localDatastore:

UIImagePickerController

然后在viewDidLoad中我将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()
    }
}

最后,在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) } } } UICollectionViewCell`的dataSource方法中,使用Parse的图像数组。

UICollectionView I am then populating the

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

  

2015-09-19 15:49:35.216 SecureSearchv3 [17816:700076] [错误]:抓住" NSInvalidArgumentException"有理由" ***    - [_ NSPlaceholderData initWithContentsOfFile:options:error:]:nil file argument":

1 个答案:

答案 0 :(得分:0)

我发现了同样的问题。有一种方法(我知道这是一个旧帖子......)

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,您将保存在localdatastore store" tmp.png"或者你称之为的任何路径,这样回复起来很简单。