I am working on an app using Parse as backend. Whenever I try to load an image the NSData that I get is not empty but whenever I try converting it to an UIImage the image I get is nil.
I already checked all the images on Parse and I can see/get all of them from the database. I also re-uploaded the images just in case something was wrong with them.
Here is the piece of code I get the NSData and convert it to UIImage:
let myImage = object.objectForKey("imagem") as! PFFile
myImage.getDataInBackgroundWithBlock({(imageData : NSData?, error : NSError?) -> Void in
if error == nil {
if let imageData = imageData{
if let newImage = UIImage(data: imageData){
myClass.imagem = newImage
}
}
}
self.collectionView?.reloadData()
})
try this code. I hope it will help you.
let userImageFile = anotherPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock
{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil
{
if let imageData = imageData
{
let image = UIImage(data:imageData)
}
}
}
For more details , you can refer parse documentation. see this https://www.parse.com/docs/ios/guide#files-images
It turns out the way I was doing was correct, but I was working through a proxy and whenever the data was being sent to my app it was not returning the full NSData. So the problem lies within the proxy and not the code. Thanks for the replies.
Try using following code snippet
if let imageData = imageData
{
var newImage : UIImage! = UIImage(data: imageData!)
if image
{
myClass.imagem = newImage
}
}
Related
I'm building an app that requires the user to have a photo. What I'm trying to do is autosave the placeholder photo until they choose the camera/photo gallery and choose a pick. My problem is that it's not happening. I've used the code from the Parse documentation as well as from my own choose photo source code that works. It still will not automatically save the photo when no photo is detected. I know finding nil and/or data in Parse is complicated. The problem may also be how I'm establishing my photo.image in the first place. If you have ideas on how to get my photo to save when a user doesn't have one please help. Here is my code.....
if let userPicture = PFUser.currentUser()?["userPhoto"] as? PFFile {
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if !(error != nil)
{
if imageData != nil
{
self.profileIMG.image = UIImage(data: imageData!)
}
else
{
let image = UIImage(named: "blueplaceholder2.png")
let imageData = UIImageJPEGRepresentation(image!, 0.25)!
let imageFile = PFFile(name:"image.png", data:imageData)
let user = PFUser.currentUser()
user!.setObject(imageFile, forKey: "userPhoto")
user!.saveInBackground()
}
}
}
}
First, to be simple, how do I change a blank UIImage view to an image I have stored in Parse? This is my code so far
var query = PFQuery(className:"Content")
query.getObjectInBackgroundWithId("mlwVJLH7pa") {
(post: PFObject?, error: NSError?) -> Void in
if error == nil && post != nil {
//content.image = UIImage
} else {
println(error)
}
}
On top of just replacing the blank UIImageView, how may I make the image that it is replaced with random? I assume I can't use an objectId anymore, because that is specific to the row that it represents.
I would first retreive the objectIds from parse with getObjectsInBackgroundWithBlock, and then select a random objectId from that array in a variable called objectId. That way you save the user from querying every object from parse and using a lot of data doing it.
Second I would
getObjectInBackgroundWithId(objectId)
if error == nil {
if let image: PFFile = objectRow["image"] as? PFFile{
image.getDataInBackgroundWithBlock {
(imageData: NSObject?, error: NSError?) Void in ->
if let imageData = imageData {
let imageView = UIImageView(image: UIImage(data: imageData))
}
}
}
At least this works for me.
First off, you'll want to use query.findObjectsInBackgroundWithBlock instead of using query.getObjectInBackgroundWithId.
This will get you all of your PFObjects. Grab a random PFObject from the array it returns and now you have a single random PFObject that you can set the content.image to.
Let's say your PFObject has an 'image' attribute as a PFFile (what you should be using to store images with Parse.)
Simply convert the PFFile into a UIImage by doing something similar to the following:
if let anImage = yourRandomPFObject["image"] as? PFFile {
anImage.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
let image = UIImage(data:imageData)
content.image = image
}
}
How to load file(it is image) from www.parse.com in swift ios application? I tried as stated on the parse.com but does not work. There is an error "'PFFile?' is not convertible to 'StringLiteralConvertible'". Please tell me how to upload multiple images.
if let imageNews = object["imageNews"] as? PFFile {
cell!.imageView.image = UIImage(named: "placeholder.jpg")
cell!.imageView.file = thumbnail
}
I found a solution of my error. This is below. Thank everybody.
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
})
imageFromParse!.getDataInBackgroundWithBlock(..)
I recommend you to use PFImageView to load files, especially when you load images for cell or you will have a problem with canceling loading image, as your cell are reusing.
You still should use the check for optional value: if let imageNews = .. , in case your object are not loaded.
if let imageNews = object.objectForKey("imageNews") as? PFFile {
cell!.imageView.image = UIImage(named: "placeholder.jpg")
cell!.imageView.file = thumbnail
}
You can find more here: http://blog.parse.com/learn/engineering/loading-remote-images-stored-on-parse/
I have some code that creates an NSSecureCoding variable called "content" and I want to convert that variable into NSData that can then be made into a UIImage or be sent to a local server. How do I convert this properly? I want this for a Share Extension I am making in my iOS app, so when you press share on a photo, it gets the photo contents and converts it into NSData. Here is my code:
inputItem = extensionContext!.inputItems.first as NSExtensionItem
attachment = inputItem.attachments![0] as NSItemProvider
if (attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String)){
attachment.loadItemForTypeIdentifier(kUTTypeImage as String,
options: nil,
completionHandler: {(content, error: NSError!) in
//insert code to convert "content"(NSSecureCoding) to NSData variable
})
}
DispatchQueue.global().async {
attachment.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil, completionHandler: { (item, error) in
if let error = error {
print(error.localizedDescription)
return
}
var image: UIImage?
if item is UIImage {
image = item as? UIImage
}
if item is URL {
let data = try? Data(contentsOf: item as! URL)
image = UIImage(data: data!)!
}
if item is Data {
image = UIImage(data: item as! Data)!
}
if let image = image {
DispatchQueue.main.async {
// image here
}
}
})
}
kinda late but this happened to me today and I solved it like this,
inside the completionHandler:
if let data = content {
self.imageData = UIImage(data: NSData(contentsOfURL: data as! NSURL)!)
}
imageData is UIImage type.
I was wondering if anyone can help me, I'm new to app developing
I am uploading images from my app to parse with no problem with help from parse documentation.
let imageData = UIImagePNGRepresentation(scaledImage)
let imageFile: PFFile = PFFile(data: imageData)
var userPhoto = PFObject(className: "postString")
userPhoto["imageFile"] = imageFile
userPhoto.saveInBackground()
but when i add the code to retrieve the image back, I'm a bit lost :/
let userImageFile = anotherPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
let image = UIImage(data:imageData)
}
}
where is the "anotherPhoto" coming from ? Parse did say "Here we retrieve the image file off another UserPhoto named anotherPhoto:"
anotherPhoto would be an instance of your postString (userPhoto in your upload example). The file downloading example that would work for you is like this:
let userImageFile = userPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
let image = UIImage(data:imageData)
}
}
Any PFFile must be referenced from a normal PFObject or it cannot be retrieved. PFFile objects themselves will not show up in the data browser.