I'm trying to use this code
if let userPicture = object.valueForKey("Image")! as! PFFile {
userPicture.getDataInBackgroundWithBlock({
(imageData: Data!, error: Error!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
self.ImageArray.append(image)
}
})
}
I got this error
Cannot convert value of type '(Data!, Error!) -> Void' to expected
argument type 'PFDataResultBlock?'
I Solved my problem
Swift 3 :
if let userPicture = object.valueForKey("Image")! as! PFFile {
userPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
let image = UIImage(data: imageData!)
if image != nil {
self.imageArray.append(image!)
}
})
}
Related
Tell me how to get rid of the error in my method? has many options tried and still the image does not display on ViewContoller.
func detailObject() {
let query = PFQuery(className: "soccer")
query.findObjectsInBackground { (objects:[PFObject]?, error:Error?) in
if error == nil {
for objects in objects! {
let detailPrognozS = objects["detailPrognozS"] as! String
let detailTitleS = objects["detailTitleS"] as! String
let detailTextS = objects["detailTextS"] as! String
let imageDetail = objects["detailImageS"] as? PFFile
DispatchQueue.main.async { [unowned self] in
self.prognozDetail.text = detailPrognozS
self.textView.text = detailTextS
self.titleDetail.text = detailTitleS
}
imageDetail?.getDataInBackground(block: { (data:Data?, error:Error?) in
if error == nil {
if let imageData = data {
DispatchQueue.main.async { [unowned self] in
self.imageDetail.image = UIImage(data: imageData)
}
}
}
})
}
}
}
The code displays this error:
Fatal error: unexpectedly found nil while unwrapping an Optional value Error be in this line"self.imageDetail.image = UIImage(data: imageData)" and the app crashes...please tell me.I beg you...
Ok, I have looked at questions like How do you access an object's fields from a Parse query result in Swift? but the answer is not working in Swift 3. With the following trying to get an image from the first PFObject in Parse I get the error:
Cannot convert value type NSData, NSError -> Void to expected
PFDataResultBlock ?
var query = PFQuery(className: PARSE_CLASS!)
query.order(byDescending: "createdAt")
query.findObjectsInBackground {
(objects, error) -> Void in
if error == nil {
//print(objects?.first?["testTxt"] as! NSString)
//print(objects?.first?["testImg"] as! PFFile)
let thumbnail = objects?.first?["testImg"] as! PFFile
thumbnail.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let image = UIImage(data: imageData!) {
//get image here
}
}
}
}
I have tried changing the type and everything. How can I store an image from Parse in recent Swift?
You can try this:
if let validObjects = objects {
for object in validObjects {
let thumbnail = object["testImg"] as? PFFile
thumbnail?.getDataInBackground (block: { (data, error) -> Void in
//read image here
}
}
}
I was trying to convert from Swift 1 to 2 in the line if let jsonData Array = try NSJSONSerialization... because it was originally giving me an error from the "extra argument 'error' in call" so I added the "try" before NSJSONSerialization but now it's saying "Invalid conversion from throwing function of type '(NSData!, NSError!) throws -> ()' to non-throwing function type '(NSData!, NSError!) -> Void'. I think the error isn't handled properly but I don't know how to do that. Here's the code
httpHelper.sendRequest(httpRequest, completion: {(data:NSData!, error:NSError!) in
if error != nil {
let errorMessage = self.httpHelper.getErrorMessage(error)
let errorAlert = UIAlertView(title:"Error", message:errorMessage as String, delegate:nil, cancelButtonTitle:"Ok")
errorAlert.show()
return
}
var error: NSError?
// let jsonDataDict = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &error) as! NSDictionary
// let jsonDataDict = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0)) as! NSDictionary
if let jsonDataArray = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0)) as? NSArray! {
//load collection view with selfies
if jsonDataArray != nil {
for imageDataDict in jsonDataArray {
var selfieImgObj = SelfieImage()
selfieImgObj.imageTitle = imageDataDict.valueForKey("title") as! String
selfieImgObj.imageId = imageDataDict.valueForKey("random_id") as! String
selfieImgObj.imageThumbnailURL = imageDataDict.valueForKey("image_url") as! String
self.dataArray.append(selfieImgObj)
}
self.collectionView?.reloadData()
}
}
})
}
First, you should be wrapping any functions that call throws in a do-try-catch loop. Secondly, safely unwrap the jsonArray as a Swift Array rather than trying to force cast it to an NSArray:
do {
if let jsonArray = try NSJSONSerialization.JSONObjectWithData(NSData(), options: .AllowFragments) as? [AnyObject] {
// unarchived data is an array
}
} catch {
print(error)
}
Trying to get an image from parse and set a uiimage with it but keep getting this error. Running Xcode 7 and swift 2.0
Cannot invoke 'getDataInBackgroundWithBlock' with an argument list of type '(imageData:NSData,error:NSError.Type,()->())'
let query: PFQuery = PFQuery(className: "Items")
query.whereKey("ItemOwner", equalTo: "Shreddish")
// 3
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
print("Successfully retrieved: \(objects)")
for object in objects! {
print(object)
let imageFile: PFFile = object["ItemMainImage"] as! PFFile
var image = imageFile.getData()
imageFile.getDataInBackgroundWithBlock(imageData: NSData, error: NSError) {
}
}
} else {
print("Error: \(error) \(error!.userInfo)")
}
}
Change
imageFile.getDataInBackgroundWithBlock(imageData: NSData, error: NSError) {
...
}
To
imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
...
}
This is my saving image to parse:
func uploadPost(){
var imageText = self.imageText.text
if (imageView.image == nil){
println("No image uploaded")
}
else{
var posts = PFObject(className: "Posts")
posts["imageText"] = imageText
posts["uploader"] = PFUser.currentUser()
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
//**Success saving, now save image.**//
// Create an image data
var imageData = UIImagePNGRepresentation(self.imageView.image)
// Create a parse file to store in cloud
var parseImageFile = PFFile(name: "upload_image.png", data: imageData)
posts["imageFile"] = parseImageFile
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
// Take user home
println("Data uploaded")
}
else{
println(error)
}
})
}
else{
println(error)
}
})
}
}
How can I load the images from parse? This is how my Parse.com "Posts" data looks like:
Any suggestions?
I think maybe something like using this:
self.imageView.sd_setImageWithURL(url, completed: block)
But I donĀ“t know how I get the URL. And what if the images has different names?
try something like this
let imageFile = pfObject["imageFile"] as? PFFile
if imageFile != nil{
imageFile!.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.imageView.image = UIImage(data: imageData)!
}
}
}
}
Where pfObject is the reference to your object. There are other ways you could check for a nil value, but this should work.
As long as you have a reference to the correct object (which you can get via a query for objectId) then you should only need the name of the column the image is file is stored in and not the image file itself.