Swift Image retrieving from Parse sdk - getting crashed
I managed to recreate the error, which seems to be some kind of memory leak / zombie on a PFObject. I'm not sure exactly why, but refactoring your code in the following manner got rid of the error in my case:
func loadImages() {
var query = PFQuery(className: "Images")
query.orderByDescending("objectId")
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
self.getImageData(objects as [PFObject])
}
else{
println("Error in retrieving \(error)")
}
})//findObjectsInBackgroundWithblock - end
}
func getImageData(objects: [PFObject]) {
for object in objects {
let thumbNail = object["image"] as PFFile
println(thumbNail)
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
println(image)
}
})//getDataInBackgroundWithBlock - end
}//for - end
}
EDIT: Incidentally, this also works:
func loadImages() {
var query = PFQuery(className: "Images")
query.orderByDescending("objectId")
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
let imageObjects = objects as [PFObject]
for object in objects {
let thumbNail = object["image"] as PFFile
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
println(image)
}
})//getDataInBackgroundWithBlock - end
}//for - end
}
else{
println("Error in retrieving \(error)")
}
})//findObjectsInBackgroundWithblock - end
}
This would indicate that the error was due to the following line:
for object : PFObject! in objects as [PFObject] {
Rewriting that line as follows:
for object : PFObject in objects as [PFObject] {
Also removes the error. So the reason for this error seems to be that that you told the program to unwrap something that wasn't an optional.
Swift Image retrieving from Parse sdk
Given that the error is happening around these lines:
self.timelineImages.append(finalimage!)
self.timelineimage.image = finalimage?
I would suggest it is either finalimage
, timelineImages
or timelineimage
.
For finalimage I would use if let
:
if let finalimage = UIImage(data: imageData) {
self.timelineImages.append(finalimage)
self.timelineimage.image = finalimage
}
This safely runs the code only if finalimage got a value.
If one of the others is coming back as nil you can solve that with ?
. thus your final code looks like this:
if let finalimage = UIImage(data: imageData) {
self.timelineImages?.append(finalimage)
self.timelineimage?.image = finalimage
}
Retrieving Parse Images Are Out of Order Swift
I had integrated something similar using Parse.
You don't need to fetch all the images at first. You can use a third party library SDWebImageCache
for downloading the image when needed or caching.
Have the postedImage
type as PFFile
and assign the imageFile directly. No need of fetching the imageData.
Have another key called updatedAt
in ImagePost
class. No need of using predicate when querying the objects from Parse
. Save the updatedAt
time of ImagePost
class. So now you can directly append the data to arrayOfUserPosts
.
After completion of the loop, you can sort the array and assign it to self.arrayOfUserPosts
.
Then in tableView's dataSource tableView:cellForRowAtIndexPath:
method, you can do something like,
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:file.url] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// Any custom view initialisation if needed.
}
where imageView is your display view for image, file is the object of type PFFile which represent the image. This is a Objective-C code. Similar syntax for Swift.
Swift 3: cant get image from Parse?
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
}
}
}
Retrieve Image from Parse in Swift
Since it's skipping the if
block, sticker!.objectForKey("imageFile") as? NSData
must be nil
. It might be nil for a few possible reasons. First, make sure your Sticker class actually has an "imageFile" property. Second, make sure object "WGYIYs0crU"
actually has data saved in that property. You can easily check those things by logging into the parse web console.
However, I suspect the problem is that you are trying to downcast to NSData
. Files are usually saved on Parse as PFFile
, so try casting to PFFile
, and skip the line where you create a new PFFile
. Something like this:
var query = PFQuery(className:"Sticker")
query.getObjectInBackgroundWithId("WGYIYs0crU") {
(sticker: PFObject?, error: NSError?) -> Void in
// Also, checking for nil isn't really necessary, since that's what if let does.
if let stickerImage = sticker?["imageFile"] as? PFFile {
file.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if let imageData = imageData {
let image = UIImage(data:imageData)
self.MirrorImageView.image = image
}
if let downloadError = error {
println(downloadError.localizedDescription)
}
}
}
if let imageError = error {
println(imageError.localizedDescription)
}
}
Hope that helps.
iOS Swift: Images retrieved from Parse.com are not in sequence
This is likely because you're fethcing the images asynchronously within an already asynchronous block, so that when you append the images to the arrays, this process of downloading the images will always take longer than just fetching the name and detail of the PFObject
. In order to circumvent this, you can try to self.customer.addObject(fName)
and self.customerDetails.addObjects(fDetail)
inside your if error == nil
conditional check.
Related Topics
iOS 13.1 Crash in Avaudio Player
Nsdateformatter Milliseconds Bug
Firebase with Swift 3 Counting the Number of Children
Add "Edit in Excel" or "Edit Photo" Extension
In Firebase, How to Query the Most Recent 10 Child Nodes
How to Apply a Vignette Cifilter to a Live Camera Feed in iOS
Is There an iPhone Se Simulator for Xcode 11, iOS 13
-Canopenurl: Failed for Url: "Fbauth2:/" (Osstatus Error -10814.)"
Firebase Cloud Messaging - Send Message to All Users
Passing Parameters to a Method Called by Nstimer in Swift
Can Not Save File Inside Tmp Directory
Smart-Search for Parse Usernames in Swift Not Working
Save Image Data to SQLite Database in Iphone
Could Not Load the "" Image Referenced from a Nib in the Bundle with Identifier
Instantiate View Controller from Storyboard VS. Creating New Instance
How to Resize an Image or Done as a Nsattributedstring Nstextattachment (Or Set Its Initital Size)