gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:57:40 24 4
gpt4 key购买 nike

我有一个 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 的数据源方法中,我用来自 Parse 的图像数组填充UICollectionViewCell`。

 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] [Error]: Caught "NSInvalidArgumentException" with reason "*** -[_NSPlaceholderData initWithContentsOfFile:options:error:]: nil file argument":

最佳答案

我发现了同样的问题。有一种解决方法(我知道这是一篇旧文章...)

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

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”或您称之为的任何路径中,以便轻松调用。

关于ios - 在 Parse 和 Swift 中使用本地数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com