Display Image From Parse In Swift - swift

I am trying to display the image I have stored in Buddy For Parse into a UIImageView, however I keep getting this error:
Could not cast value of type 'PFFileObject' (0x1045e0568) to 'NSString' (0x1041d75d8).
2019-04-13 18:15:09.869460-0500 PerfectLaptop[43839:3094232] Could not cast value of type 'PFFileObject' (0x1045e0568) to 'NSString' (0x1041d75d8).
I have already stored numerous strings into Parse, and am able to access them with no problems, and have stored the image I wanted to use also, however no matter what I try, I can't seem to get it to work. Many of the solutions I have found include casting the object as a PFFile, however this doesn't seem to exist anymore.
let query = PFQuery(className: "whichOneRecommended")
query.findObjectsInBackground { (objects, error) in
if error == nil
{
if let returnedobjects = objects
{
for object in returnedobjects
{
if (object["whichOne"] as! String).contains("\(self.whichOneJoined)")
{
self.laptopImage.image = UIImage(named: (object["laptopImage"]) as! String)
}
}
}
}
}
While the image file is viewable and downloadable in parse, I can't seem to actually have it be displayed in the imageview, and i want the image view to change programmatically by running this function as I have with other parts of the object.
Thanks in advance

The first thing to note is that PFFile has been renamed to PFFileObject.
You are trying to pass object["laptopImage"] which is a value of type Any to UIImage(named:) which can't be done because that function expects a String.
Firstly you need to create a constant of type PFFileObject:
let file = object["laptopImage"] as? PFFileObject
And then download the file data, create a UIImage from the PFFileObject and assign the image to the UIImageView:
file.getDataInBackground { (imageData: Data?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let imageData = imageData {
let image = UIImage(data: imageData)
self.laptopImage.image = image
}
}
Details on this can be found in the section on files in the iOS Guide.

guard let imageString = message,
let imageURL = URL(string: imageString) else { return }
do {
let imageData = try Data(contentsOf: imageURL)
image.image = UIImage(data: imageData)
} catch {
}

Related

Why is UIImage? nil here?

I'm trying to bridge React and Swift code by passing a string for an image path, which I've verified appears correctly on the Native side, and having a bit of an issue. The image path comes from React as NSString, and my goal is to pass that as a String to a Native function that will ultimately send data back to React.
Here's a snippet of some code that handles part of this
classifyImage(value as String)
and some of the body of the classifiyImage is as follows:
#objc func classifyImage(_ image: String) {
let imageData = Data(base64Encoded: image, options: .ignoreUnknownCharacters)!
let uiImage = UIImage(data: imageData)
guard let orientation = CGImagePropertyOrientation(
rawValue: UInt32((uiImage?.imageOrientation.rawValue)!)) else {
return
}
...code
}
The exact error is at the line with the rawVale, reading
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Here's more info if it may help...
Image data can come from the camera as such image NSMutableString "file:///var/mobile/Containers/Data/Application/54691469-2196-444E-9B45-C0D6F2CABEBC/Library/Caches/Camera/EEC3631C-3E96-44DA-B258-411363A2F10C.jpg" 0x00000002815a8420
or from the phone's gallery image String "ph://8F109DC0-CE95-4D0A-9D11-1B2E9CE6B8D3/L0/001"
Image from a file
First, we need to turn the string into a URL, then the URL into data like so:
let url = URL(string: image)
do {
let data = try Data(contentsOf: url)
} catch {
print(e)
}
Then we can use it to create the image.
do {
let data = try Data(contentsOf: url)
let image = UIImage(data: data)
guard let orientation = CGImagePropertyOrientation(
rawValue: UInt32(image.imageOrientation.rawValue)) else {
print("that didn't work")
return
}
} catch {
print(e)
}

Saving UIImage Into Core Data Always Returns Nil when using ImagePickerView - Swift 3

I've been trying to save a single picture to an entity containing a single property titled "prof" and configured as a Binary Data type.
I go through the hoops to select a picture from UIImagePickerViewController, then I call up my method that handles saving the picture in Core Data in the desired NSData format.
My issue stems from loading the picture, in my loadImage method the entity for the image is not nil, meaning it does exist. However, I get nil when I try to parse the fetched NSData to a UIImage format to recreate the picture and then be able to use it.
Now i am using Swift 3 and Xcode 8, so far all the troubleshooting questions on here have the solution of casting the NSData to UImage like so:
let image : UIImage = UIImage(data: imageData)
however, xcode gives me a compiler error when I do this, and instead forces me to cast it as:
let image : UIImage = UIImage(data: (imageData as Data?)!)
which is where i get the nil that's throwing up my flow in the air... i've tried saving the data in many different ways, but still nothing.
if anyone could go through my following methods, see if i might be doing something wrong in the saving part, or the formating of NSData on the fetch method... anything would help.
My configuration:
-the prof property has "Allow external storage" set to true
-my persistent store is seeded blank at the app installation, meaning all the needed properties are already set up when the app is launched for the first time, but obviously set to nil until changed or modified by my various data flows.
-There is no other picture entity in my data model, this is the only one.
func saveProfilePicture(_ pic: UIImage){
let picData = UIImagePNGRepresentation(pic)
let request: NSFetchRequest<UsePics> = UsePics.fetchRequest()
do {
let records = try coreDataManager.managedObjectContext.fetch(request) as [UsePics]
let first = (records.first)
first?.setValue(picData, forKey: "prof")
try context.save()
} catch let err {
print(err)
}
}
func getProfilePicture() -> UIImage? {
let request: NSFetchRequest<UsePics> = UsePics.fetchRequest()
var image : UIImage?
do {
let records = try coreDataManager.managedObjectContext.fetch(request) as [UsePics]
let first = (records.first?.prof) as NSData
if let parsedImage : UIImage = UIImage(data: (first as Data?)!) as? UIImage {
image = parsedImage
}
} catch let err {
print(err)
}
return image
}
EDIT
The solution was found by noticing that in Swift 3, the UIImage class adheres to the Transformable protocol. Swapping my property type for the image from Binary Data to Transformable actually made it possible to save the UIImage as UIImage directly into Core Data without parsing it to another data type.
func saveProfilePicture(_ image: UIImage){
let request: NSFetchRequest<UsePics> = UsePics.fetchRequest()
do {
let records = try coreDataManager.managedObjectContext.fetch(request) as [UsePics]
let first = (records.first)
first?.prof = image
print(first)
coreDataManager.saveData()
} catch let err {
print(err)
}
}
func loadProfilePicture() -> UIImage? {
var image : UIImage?
let request: NSFetchRequest<UsePics> = UsePics.fetchRequest()
do {
let records = try coreDataManager.managedObjectContext.fetch(request) as [UsePics]
let first = records.first
if let img = first?.prof {
image = img as? UIImage
} else {
print("no image")
}
} catch let err {
print(err)
}
return image
}

How to retrieve image from parse and display it in a UIImageView in swift?

So I have been trying to retrieve an image from Parse since yesterday and I am very confused. I read similar questions and examples but all of them were people saving some image first, then retrieving it. What I want to do is simply retrieve an image that I manually uploaded to parse, I have been trying with this:
//Retrieving image from parse
var object = PFObject(className:"ClassImage")
let getParseImg = object["image"] as! PFFile
getParseImg.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if (error == nil){
if let imageData = imageData{
println("Retrieving Image")
let img = UIImage(data:imageData)
self.imageView.image = img
}
}
}
In line two, it throws a "unexpectedly found nil while unwrapping an Optional value", I already tried "if let getParseImg", then it throws another error, in the end the line ended up looking like this:
if let getParseImg = object["image"] as? PFFile{
So after that, everything goes fine and compiles but it doesn't do absolutely anything, it does not get and display the image and it doesn't even print "Retrieving Image", any ideas how can I solve this, I am new to Parse so still getting familiarized with it.
Thanks in advance!
You need to run a query. Also, I used a dictionary called imagesDict in order to ensure your photos are returned in the correct order.
var query = PFQuery(className: "ClassImage")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
let imageObjects = objects as! [PFObject]
for (index, object) in enumerate(imageObjects) {
let thumbnail = object["image"] as! PFFile
thumbnail.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let image = UIImage(data: imageData!) {
self.imagesDict[index] = image
self.theTableView.reloadData()
}
}
}
}
}
}

Retrieving an image from Parse with Swift

I'm trying to retrieve an image and text from Parse. I'm able to retrieve the saved text but not the image. What am I doing wrong? Below is the code for the function that I want to retrieve the image. Thanks in advance.
func showImage() {
var query = PFQuery(className: "Description")
query.orderByDescending("ceatedAt")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
self.imageArray = [UIImage]()
if let objects = objects {
for imageObject in objects {
let userImage: UIImage? = (imageObject as! PFObject)["UserPhoto"] as? UIImage
if userImage != nil {
self.imageArray.append(userImage!)
}
}
}
self.tableView.reloadData()
}
}
It's tricky at first, but it gets a lot easier. Here's the code to make it work:
let userImage = (imageObject as! PFObject)["UserPhoto"] as! PFFile
userImage.getDataInBackgroundWithBlock {
(imageData, error) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
self.imageArray.append(userImage!)
} else {}
}}
the issue is that parse stores images as PFFiles, they're not directly images yet, think of it more as a URL than anything. You have to download the image, and then do something with it to make it work. You can't just directly cast it as a UIImage.
One thing to note (because this gave me trouble a while ago) is that the .getDataInBackgroundWithBlock method is asynchronous, so it'll run on it's own, and your code will continue before it's completed. Another thing to get used to.
Best of luck!

Error when retrieving PFFile via getDataInBackgroundWithBlock

I have created a var userImages = [PFFile]() and have appended the respective user images from Parse via a user query and self.userImages.append(user["imageFile"] as! PFFile). This works fine. However, when I try to set the image of the user via
userImages.getDataInBackGroundWithBlock{ (data, error) -> Void in ...
I'm receiving the following error: **'[(PFFile)]' does not have a member named 'getDataInBackGroundWithBlock'**
Why doesn't this work and what might be a solution for this issue?
Thank you for the help!!
You are trying to call getDataInBackGroundWithBlock in a array try to use"
userImages.last?.getDataInBackGroundWithBlock{...}
or you can save it in a variable and use the new PFFIle to retrieve the image
let userImage = user["imageFile"] as! PFFile
userImage.getDataInBackGroundWithBlock{...}
or access it directly using:
(user["imageFile"] as! PFFile).getDataInBackGroundWithBlock{...}
The role process being:
self.userImages.append(user["imageFile"] as! PFFile) //This is just a reference to download the image
userImages.last?.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData) // this is the final image
}
}
}