I've a string in which I've comma separated links of images. Here is how I'm splitting it into an array: let imagesLinks = imageLins.components(separatedBy: ","). Then I've used for loop to get one link, download the image and storing it in a UIImage array in this way:
for imag in imagesLinks
{
let img = UIImageView()
print("\(baseReportImageURL)\(imag)")
img.sd_setImage(with: URL(string: "\(baseReportImageURL)\(imag)"), placeholderImage: nil)
imagesArray.append(img.image!)
}
The print statement is giving me the correct URL which when I open on browser downloads the image. The problem is on the line where I'm appending the array i.e. imagesArray.append(img.image!). I get:
fatal error: unexpectedly found nil while unwrapping an Optional value
and
fatal error: unexpectedly found nil while unwrapping an Optional value
So what would be the correct solution for this?
UPDATE
My question is different because I'm using SDWebImage and when I use completion block there is a strange behaviour of the app:
img.sd_setImage(with: imgURL, placeholderImage: nil,options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
activityIndicator.stopAnimating()
imagesArray.append(image!)
self.photoCollection.reloadData()
})
So it keeps on rotating the activity indicator and when I go back and push the view again it load the images instantly. So I think that the completion block is not called when the image is downloaded but why is that?
I think that is not the proper way to get downloaded images (set images to an UIImageView and then get the images from there).
You should use the image downloader, provided by SDWebImage:
SDWebImageManager.shared().imageDownloader?.downloadImage(with: <YOUR_URL>, options: [], progress: { (received, expected, nil) in
print(received,expected)
}, completed: { (image, data, error, true) in
yourArray.append(image)
})
If you have indexes, you can update the current row in the tableview every time when an image downloaded, or just reload() the whole, but I recommend the first.
It is because
imagesArray.append(img.image!)
Image take some times for downloading. When you do imagesArray.append(img.image!) at that time it is possible that your image is not set to your imageview and that means you are trying to add nil in your imageArray!
Second thing why are you storing image in an array ? You have array of urls and you are using SDWebImage then every time when you want to display image use SDWebImage. No need to store in array!
And if you want images in array anyhow than use NSUrlSession asynchronous requests with completion handlers and from completion handler add image to your array!
Append the image into the images array in the sd_setImage completion block.
The image is not yet downloaded when you are trying to add it into the array, and check if the image is not equal nil before adding it.
Use this Block For Image Download After download Perform Operation(append Image)
cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
// Perform operation.
//append Image Here
})
Related
I'm using SDWebImage library to cache web images in my UICollectionView:
cell.packItemImage.sd_setImage(with: URL(string: smileImageUrl[indexPath.row]))
but I want to save the cached images locally in a file instead of downloading them again
FileManager.default.createFile(atPath: newPath, contents: Data(contentsOf: URL(string: snapchildvalue[Constants.smiles.smileImageUrl] as! String)!), attributes: nil)
is there a way to get the data of cached images
SDWebImage caches downloaded images automatically by default. You can use SDImageCache to retrieve images from the cache. There is a memory cache for the current app session, which will be quicker, and there is the disk cache. Example usage:
if let image = SDImageCache.shared().imageFromDiskCache(forKey: imageURL.absoluteString) {
//use image
}
if let image = SDImageCache.shared().imageFromMemoryCache(forKey: imageURL.absoluteString) {
//use image
}
Also make sure you import SDWebImage in your file. (If you're using Swift/Carthage, it will be import WebImage
SDWebimage chaches image once it is downloaded from a url. Basically it saves image against a url and next time if an image is available for a URL. It will simply get that image from cache. So the below method will be called instantly if the image is already downloaded to device.
imgView.sd_setImage(with: URL(string:url), completed: { (image, error, type, url) in
imgView.image = image
//Do any thing with image here. This will be called instantly after image is downloaded to cache. E.g. if you want to save image (Which is not required for a simple image fetch,
//you can use FileManager.default.createFile(atPath: newPath, contents: UIImagePNGRepresentation(image), attributes: nil)
})
Still if you want to save that image somewhere else or modify it or whatever, you can do it in the completion block above.
SDWebImage already have this kind of caching file locally
Create a SDImageCache with namespace of your choice
Try get the image with imageCache.queryDiskCache
If the image exist, set it to your imageview, if not, use sd_setImage to get the image then save it to the local cache with SDImageCache.shared().store
The key usually to be the image url string
Something like this, might not be correct syntax:
imageCache.queryDiskCache(forKey: urlForImageString().absoluteString, done: {(_ image: UIImage, _ cacheType: SDImageCacheType) -> Void in
if image {
self.imageView.image = image
}
else {
self.imageView.sd_setImage(withURL: urlForImageString(), placeholderImage: UIImage(named: "placeholder")!, completed: {(_ image: UIImage, _ error: Error, _ cacheType: SDImageCacheType, _ imageURL: URL) -> Void in
SDImageCache.shared().store(image, forKey: urlForImageString().absoluteString)
})
}
})
OverView
I am downloading the images from web server using SDWebImage in my collectionView cell.
if floor.hasTitleImage != nil {
self.floorImageView.sd_setImageWithURL(NSURL(string:(floor.hasTitleImage?.imageURL)!), placeholderImage: UIImage(named: "Placeholder"), completed: { (imageDownloaded, error, cacheType, url) in
if imageDownloaded.size.width > 300 {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let resizedImage = imageDownloaded.resizePreservingAspectRatio(toSize: CGSize(width: 300, height: 300))
SDImageCache.sharedImageCache().removeImageForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, fromDisk: true, withCompletion: {
SDImageCache.sharedImageCache().storeImage(resizedImage, forKey: NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, toDisk: true)
})
})
}
})
}
else{
self.floorImageView.image = UIImage(named: "Placeholder")
}
Now what this code is doing is downloading the image using the url stored in core data element called floor. Now the images downloaded are way bigger then my ImageView so am resizing it to somewhere around 300px.
I know that SDWebImage does intense caching by default and uses url's absolute string as key for its cache. So Once resized! I replace the original image with my resized image.
Issue
Everything worked fine as expected! till I tested it with low internet speed which caused the image download to be delayed.
If the image downloading takes time, till then if I scroll the tableView and the cell gets reused, once the image downloaded SDWebImage loads the imageView of the cell but ends up resulting the wrong cell with wrong image.
Though this happens only once as for next time onwards image will be loaded from cache everything works fine. But Is this the expected behaviour or am I doing something wrong???
Solutions I could think of :
As sd_setImageWithURL by default sets the value of imageView with downloaded image rather than downloading the image using sd_setImageWithURL, if I can download it using,
if floor.hasTitleImage != nil {
if let downloadedImage = SDImageCache.sharedImageCache().imageFromMemoryCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString){
self.floorImageView.image = downloadedImage
}
else if let diskImage = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString){
self.floorImageView.image = diskImage
}
else{
let downloadManager = SDWebImageManager.sharedManager();
downloadManager.downloadImageWithURL(NSURL(string:(floor.hasTitleImage?.imageURL)!), options: [], progress: nil, completed: { (downloadedImage, error, cacheType, finished, url) in
if downloadedImage.size.width > 300 {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let resizedImage = downloadedImage.resizePreservingAspectRatio(toSize: CGSize(width: 300, height: 300))
SDImageCache.sharedImageCache().removeImageForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, fromDisk: true, withCompletion: {
SDImageCache.sharedImageCache().storeImage(resizedImage, forKey: NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString, toDisk: true)
})
if floor.hasTitleImage?.imageURL != nil && floor.hasTitleImage?.imageURL! == url{
dispatch_async(dispatch_get_main_queue(), {
self.floorImageView.image = SDImageCache.sharedImageCache().imageFromMemoryCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString)
self.reloadInputViews()
})
}
})
}
})
}
}
else{
self.floorImageView.image = UIImage(named: "Placeholder")
}
Though it works it will load the image only when I scroll the tableView :(
Questions :
Is what sd_setImageWithURL doing is expected behaviour ?? I mean every single one of the app using SDWebImage with tableView or CollectionView must have faced this correct ?? So if no body faced it I must be doing something wrong :( Am I doing anything wrong ?
Is what am doing using SDWebImageManager.sharedManager is correct ??? Is that long code required? Checking memory cache then disk cache and finally making call to download image isn't it too much :o
Why is that my second approach not loading the image once downloaded and expects the collection view to scroll and reload the cell to display the image ??
EDIT
I solved the 3rd question :) Sorry my mistake. I was comparing if floor.hasTitleImage?.imageURL != nil && floor.hasTitleImage?.imageURL! == url{ which is wrong because url above is NSURL where as floor.hasTitleImage?.imageURL! is NSString.
So Updated solution :
if floor.hasTitleImage?.imageURL != nil && floor.hasTitleImage!.imageURL! == url.absoluteString{
dispatch_async(dispatch_get_main_queue(), {
self.floorImageView.image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(NSURL(string:(floor.hasTitleImage?.imageURL)!)?.absoluteString)
self.setNeedsLayout()
})
}
Though my second approach downloads the image now and updates the cell properly but now image downloading is very slow compared to sd_setImageWithURL.
Is there any way I can use the method1 sd_setImageWithURL and get the image updated properly?
Please help me!! what am doing wrong !! Thanks in advance :)
Is what sd_setImageWithURL doing is expected behaviour ?? I mean every single one of the app using SDWebImage with tableView or CollectionView must have faced this correct ?? So if no body faced it I must be doing something wrong :( Am I doing anything wrong ?
No, it's not expected behaviour. I'm using SDWEbImage as well both tableView and collectionView and both are work fine.
Is what am doing using SDWebImageManager.sharedManager is correct ??? Is that long code required? Checking memory cache then disk cache and finally making call to download image isn't it too much :o
You don't have to cache image youself, SDWebImage take care caching process for you (in memory and disk).
If you need to resize image before cache and display, I think better do it by implement SDWebImageManagerDelegate which allows you to transform the image after it has been downloaded but before cache and display.
UPDATE:
If you use sd_setImageWithURL, there is another solution to prevent wrong assign image is by override prepareForReuse() method in UICollectionViewCell subclass and call sd_cancelCurrentImageLoad() on imageView and set image to nil.
hope this help.
all
I am learning Swift and I am trying to set an image on a UIImageView using AlamofireImage. I am using the following code:
self.listImageView.af_setImageWithURL(
NSURL(string: list!.image!)!,
placeholderImage: nil,
filter: nil,
imageTransition: .CrossDissolve(0.5),
completion:{ image in
print(image)
}
)
and the result in the console is the following:
SUCCESS: <UIImage: 0x7fb0c3ec3d30>, {512, 286}
My objective is to do something with the image once is downloaded, but the problem is that I don't understand the signature for the completion callback and I don't know how to access to the image in the completion block. According to the documentation, is Result<UIImage, NSError>.
I guess is something really simple, but I am not realizing it.
Thanks
The image variable passed into the completion block is actually Alamofire.Response type, not the underlying UIImage instance itself that was fetched.
You need to update your completion block like below in order to get the actual image from the response:
self.listImageView.af_setImage(
withURL: URL(string: list!.image!)!,
placeholderImage: nil,
filter: nil,
imageTransition: .crossDissolve(0.5),
completion: { response in
print(response.value) # UIImage
print(response.error) # NSError
}
)
You might first want to check response.result.isSuccess (or his brother response.result.isFailure) to make sure whether the image has been successfully retrieved or not.
I have two UIImageView objects on my view stored inside an array called imgViews and I try to download images for them asynchronously with this code:
func showPic(positionIndex: Int){
let urlStr = "https://www.friendesque.com/arranged/userpics/amir/1"
let url = NSURL(string: urlStr)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error == nil {
self.imgViews[positionIndex].image = UIImage(data: data)
//self.imgViews[positionIndex].image = UIImage(named: "11665489_10154101221228009_2542754962143804380_n.jpg")
print("Loading Done...")
}
else{
print(error)
}
})
task.resume()
}
and inside my viewDidLoad(), I have
showPic(0)
When I run the code, I see "Loading Done..." immediately which means the picture has been loaded but it takes a very long time (about 1 min) for the UIImageView to actually change to the loaded picture. It's a very small picture (~15K) and it can't be a processing time problem.
I tried loading a resource image (the comment part of the code) instead of the downloaded picture but it's still slow.
I'm really confused. Why is swift so slow at working with images inside a block?
Perhaps when the data task returns it is on a background thread? You will need to switch to the main thread to change a UIImageView. Regardless I would use the UIImageView+AFNetworking category to achieve this. It's simple, well tested and lets you provide a placeholder image to display while it is downloading.
https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/UIImageView%2BAFNetworking.h
to use:
myImageView.setImageWithURL(url!)
I am trying to convert a PFFile to a UIImage. It works, however, it doesn't seem to run until the very end. The line after the image is called appends the image to the array (photosArray.append(image!)), but when I do this, it doesnt work, and stores nothing. I believe this is because since the image hasnt been fully retrieved yet because it is grabbing the image in the background. How do I not have it save in the background, but wait until all the data is loaded, THEN append to the photosArray image, so the array isnt empty?
var photosArray = [UIImage]()
let photosValue = log["Images"] as! PFFile
photosValue.getDataInBackgroundWithBlock({
(imageData: NSData?, error:NSError?) -> Void in
if (error == nil) {
let image = UIImage(data:imageData!)
//line below appends nothing! photosArray is an empty array, []
photosArray.append(image!)
//following line of code doesnt run until the end. Is the last line to be printed in console.
println("Our Image: \(image)")
}
})
//prints "[]", no value
println(photosArray)
println("Our Image Array^^")
}
//after all the code is ran, the println("Our Image: \(image)") will run,
//and say "Our Image: Optional(<UIImage: 0x16f0ead0>, {394, 256})".
//This is the last line of the console logged, so it IS saving, but not until the end.
That is not a correct assumption, the whole point of the getDataInBackgroundWithBlock is to handle the response data. You also wouldn't be able to assign
let image = UIImage(data: imageData!)
if it were nil.
I'm not sure about what println statements you're referring to, however printing out the array while data is still being collected in the background may not give you a result, and just
[]
But you should see
println("Our image: \(image)")
If you plan to display these images inside a UIImageView I suggest taking a look at the ParseUI class PFImageView, which has a property you can assign a PFFile to and then a method in which to load it.