Downloading Pffile (Image) from Parse Append It to Array and Fill Uiimageview with That Array (Swift)

How to convert an PFFile to an UIImage with swift?

PFFile is a parse representation of anykind of file. To get the "real" file (image), you need to call getDataInBackgroundWithBlock. Try it:

if let userPicture = object.valueForKey("Image")! as! PFFile {
userPicture.getDataInBackgroundWithBlock({
(imageData: NSData!, error NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
self.ImageArray.append(image)
}
})
}

Convery my UIImageView to a PFFile in SWIFT

You need to take the image out of the UIImageView and sage it to a PFFile.

var file = PFFile(data: UIImageJPEGRepresentation(imageView.image, 1.0))

Then of course, you can upload the file somewhere you choose.

Retrive an image into UiimageView from Parse (Swift)

Move :

func getProductData () -> UIImage
{
var gridimage = UIImage()
print("pass");
//call function to get the data from parse by specifyng an objectId
query.getObjectInBackgroundWithId("XXXXXXXXXX") {
(productData:PFObject?, error:NSError?) -> Void in
if error == nil && productData != nil {
//Extract values from the productData PFObject and store them in constants
//-----start image loading
print("ok")
//var imageFromParse = object?.objectForKey("imageNews") as? PFFile
//imageFromParse!.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
//var image: UIImage! = UIImage(data: imageData!)!
//cell?.imageViewCell?.image = image
//})
if let productImageFile = productData!["productImage"] as? PFFile {
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
var gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")


}
}
}



} else if error != nil {
print("Could not load data from Parse")

}


}
return gridimage
}

out of viewDidLoad then delete:

print("done");
self.eliegrid.image = getProductData()

From the bottom of viewDidLoad. You are calling a function that goes out of scope before the completion block can be executed that is why it's not firing. It is not a very swift-y thing to nest functions in this manner and is almost never advisable.

Also try changing:

if let productImageFile = productData!["productImage"] as? PFFile {
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
var gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")


}
}
}

To:

                let productImageFile = productData.valueForKey("productImage") as! PFFile 
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
var gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")


}
}

iOS - Retrieve and display an image from Parse in UIImageView (Swift 2.1 Error)

I think your error is due to App Transport Security. You are fetching an image from an unsafe http url and to do that you have to enable the url in your Info.plist file.

More about ATS

Basically what you can test is to disable ATS by adding

<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

to your Info.plist file. (right click Info.plist -> Open as -> source code, and add the key and value above.)

If this works you should add a special permission for this url only and revert the previous disabling.

To just enable the specific domain is a bit trickier. You need to know a little bit about what violates the ATS to fix it. It would look something like this I imagine

<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>parse.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>

iOS - Retrieve and display an image from Parse in UIImageView (Swift 1.2 Error)

PFUser.currentUser() returns optional type (Self?). So you should unwrap return value to access element by subscript.

PFUser.currentUser()?["picture"]

And the value got by subscript is also optional type. So you should use optional binding to cast the value because type casting may fail.

if let userPicture = PFUser.currentUser()?["picture"] as? PFFile {

And parameters of the result block of getDataInBackgroundWithBlock() method are both optional type (NSData? and NSError?). So you should specify optional type for the parameters, instead NSData and NSError.

userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in

The code modified the all above problems is below:

if let userPicture = PFUser.currentUser()?["picture"] as? PFFile {
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
self.dpImage.image = UIImage(data:imageData)
}
}
}


Related Topics



Leave a reply



Submit