Placeholder image as Thumbnail(blurred) until actual image loads - swift

I have below code for showing full screen image once User selects it.
fullImage.kf.setImage(with: url)
During the time the actual image downloads, I wanted to show a place holder as the same image but thumbnail size stretched out. This thumbnail was cached by Kingfisher library while showing thumbnails previously.
When I show the image with out KF library, by using regular iOS UIImage downloader it shows a blurred preview of thumbnail stretched and then it shows actual image. How can this be achieved with KF library?

you can use Place holder image like below
fullImage.kf.setImage(with: url, placeholder: UIImage(named: "placeholder"), options: nil, progressBlock: nil, completionHandler: nil)
But to show thumbnail you must have thumbnail image downloaded separately.

If you want the thumbnail to be an image form the web, you can do what people are suggesting on GitHub.
let cacheImage = ImageCache.default.retrieveImageInDiskCache(forKey: "cache")
let resource = ImageResource(downloadURL: imageURL, cacheKey: "cache")
imageView.kf.setImage(with: resource, placeholder: cacheImage, options: [.keepCurrentImageWhileLoading], progressBlock: nil, completionHandler: nil)
You'll need to find a way to download the thumbnails first though, which can be done in multiple ways, e.g. using an ImagePrefetcher

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?

ARKit2 Marker Images and Assets On-Demand

I have built an app that works really well if all resources and assets are within the app for 2D Markers.
But now, I'd like to pull marker 2d images on-demand from server, then use the downloaded 2D images to play content related to specific images.
I am having a hard time finding a solution for this. Any ideas or feedback will be greatly appreciated.
Thank you.
You can use Alamofire.
An example from their repo:
let downloader = ImageDownloader()
let urlRequest = URLRequest(url: URL(string: "https://httpbin.org/image/jpeg")!)
downloader.download(urlRequest) { response in
print(response.request)
print(response.response)
debugPrint(response.result)
if let image = response.result.value {
print(image)
}
}

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

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()

Getting iOS 8 UIImage Orientation Property

I am loading photos from the users Photo roll.
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize:size, contentMode: .AspectFill, options: options)
{
result, info in
//get orientation???
var image:iImage = iImage(uiimage: result)
self.selectedPhotoUpdateCallback(editImage,image)
}
However, some times the photos I load are upside down.
I cannot seem to find any information on the meta data for the orientation of these images.
Do you know how I can check the orientation of the requested asset from a PHImageManager?
The picture displays upright when I am selecting from the photo roll, but when I load the full image into another view its rotated (and my code does not apply any rotation).
You can get the image orientation via image.imageOrientation. Also you can check out this link.

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.