Load Gif image using Kingfisher Swift cause laggy when scrolling down UITableView - swift

I have a tableview and each cell will load Gif file from server.
I'm using Kingfisher to load Gif file into cell imageview like this:
cell.ivPost?.kf.setImage(with: url)
the Gif is loaded successfully, however the tableview is very laggy when scrolling down. I though that the loading, encode & decode gif file should be done asynchronously using Kingfisher
Anyone has solution for this?
I'm using xCode 8 and Swift 3

User this method
public func setImage(with resource: Resource?, placeholder: Image? = default, options: KingfisherOptionsInfo? = default, progressBlock: Kingfisher.DownloadProgressBlock? = default, completionHandler: Kingfisher.CompletionHandler? = default) -> Kingfisher.RetrieveImageTask
you can give options KingfisherOptionsInfo, which is an array of
KingfisherOptionsInfoItem
/// Decode the image in background thread before using.
case backgroundDecode

I've ended up using SDWebImage and it works smoothly with SDAnimatedImageView()

Related

Determine the image orientation of image in Firebase Storage

I need to determine the image orientation of the image to be displayed in an imageView. My app stores images in Firebase Storage and uses the SDWebImage integration in Firebase like so:
imageView.sd_setImage(with: imageRef)
Thought that the metadata would hold enough information to determine image orientation, so I tried this:
imageRef.getMetadata(completion: (StorageMetadata?, Error?) -> Void)
While I'm able to retrieve the metadata, I can't seem to figure out the orientation from it. Accordning to the docs (https://firebase.google.com/docs/storage/ios/file-metadata#file_metadata_properties) image height or width cannot be obtained from metadata.
I know I could store image orientation info in my realtime database upon uploading the image, but I'd rather not go that way.
Earlier I used this
imageRef.getData(maxSize: Int64, completion: (Data?, Error?) -> Void)
which can give you an UIImage to play around with. While I can get the image orientation this way, it's too slow for my (client's) taste.
Is it possible to determine image orientation from Firebase Storage info only?

How to stop animating GIF in SDWebImage?

I'm using SDWebImage to fetch images from web and display them.
But some are GIFs, how to stop the GIF?
What you can do to stop the animation is to simply set your SDAnimatedImageView to for example
sdAnimatedImageView.image = SDAnimatedImage(named: "your.gif")?.animatedImageFrame(at: 0)
that way you will get one frame of your gif only and it will not be animated :)
its a hack but does the job
Assuming you are using >= v4.0; from the docs:
Starting with the 4.0 version, we rely on FLAnimatedImage to take care of our animated images.
From FLAnimatedImage's docs; there are 2 ways to control playback:
// An FLAnimatedImageView can take an FLAnimatedImage and plays it automatically when in view hierarchy and stops when removed.
// The animation can also be controlled with the UIImageView methods -start/stop/isAnimating.

PFImageView loadInBackground() issue in iOS10

I am trying to use PFImageView.loadInBackground() in iOS10 .
So far my code has worked in iOS9 etc.
This is what I am doing:
fetching image data
assigning data to PFImageView.file
loading file via PFImageView.file.loadInBackground()
The ImageData is found, but the image won't display.
Any ideas?
I had an issue with PFImageView when the file name for PFFile had white spaces.
Did you try running in Simulator what errors did you receive?

Saving UIImage to plist problems (SWIFT)

Simply stated,
I can encode a UIImage into a .plist if that image is selected from a UIImagePickerController (camera or photo library) and then stored into an object's instance variable using NSKeyedArchiver...
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
myObject.image = selectedImage
...
}
I can NOT, however, encode a UIImage into a .plist if that image is one existing in my app bundle and assigned to a variable like this...
myObject.image = UIImage(named: "thePNGImage")
...where thePNGImage.png lives in my apps bundle. I can display it anytime in my app, but I just can't store it and recover it in a plist!
I want a user to select a profile image from his or her camera or photo library, and assign a default image from my app bundle to their profile should they not choose to select one of their own. The latter is giving me issues.
Any help would be greatly appreciated.
Thanks!
First of all, you don't need any plist. If you want to save the user's preferences, use NSUserDefaults (which is a plist, but it is maintained for you).
Second, you should not be saving an image into a plist. Save a reference to an image, e.g. its URL. That way when you need it again you can find it again. An image is huge; a URL is tiny.
Finally, from the question as I understand it, you want to know whether the user has chosen an image and, if not, you want to use yours as a default. The NSUserDefault could contain a Bool value for this, or you could just use the lack of an image URL to mean "use the default".
But in any case you are certainly right that it is up to you to recover state the next time the app launches based on whatever information you have previously saved. Nothing is going to happen magically by itself. If your image is not magically coming back the next time you launch, that is because you are not bringing it back. Your app has to have code / logic to do that as it launches and creates the interface.
Still having the issue? BTW, I've confirmed your issue. Here's a workaround:
func imageFromXcassets(name: String) -> UIImage
{
// Load image from Xcassets
let myImage = UIImage(named: name)!
// Write image to file
let imagePath = NSHomeDirectory().stringByAppendingPathComponent("Documents/test.png")
UIImagePNGRepresentation(myImage).writeToFile(imagePath, atomically: true)
// Get image from file and return
return UIImage(contentsOfFile: imagePath)!
}
Given the name of an image stored in Images.xcassets, the method returns the UIImage after first writing the image to a file, then reading the image from the file. Now the image can be written successfully to NSUserDefaults just like images obtained from your imagePickerController.
Please note that the example isn't handling optionals like it should. (I'm an optimistic guy.)
ADDITION BELOW
Note on Leonardo’s response:
The originating question asks how to encode an image obtained from Images.xcassets via UIImaged(named: String). I don’t believe Leonardo’s response provides a solution for this.

SDWebImage: Prevent to many server request

I'm using SDWebImage framework to download profile images in my iPhone game.
[cell.playerImage setImageWithURL:url placeholderImage: DefaultImage];
This works great, but I'm worried about the server performance. Each time the tableView is reloaded, all the cell will call the the code above to try to get the image. And the tableView is realoaded quite often.
Since most player don't have a profile image, the DefaultImage will be set, but I still want to check from time to time if the opponent player has uploaded a profileImage, so I can get it, but not in each and every tableView reload.
How would you go about doing this?
Thanks in advance
Save player image in document Directory first time when it requests for image from server with names playername.png or playerID.png (varies what unique u want for that).
In SDWebImage setImageWithURL's method add this logic:
if([[NSFileManager defaultManager] fileExistsAtPath:#"Your Player Image Path"])
{
// set image from local Path
}
else
{
//request from server
// save image in document Directory and then set that image
}