how to do something right after downloading image using SDWebImage? - ios

So, I want to show a message to the user right after an image is downloaded and set in ImageView. I am using SDWebImage. how to do that?
Here is my current code
profilePictureImageView.sd_setImage(with: referenceImage, placeholderImage: UIImage(named: ImageName.defaultProfilePicture))

You can do it like this
profilePictureImageView.sd_setImage(with: referenceImage, placeholderImage: UIImage(named: "cod_logo"), options: [.highPriority]) { (image, error, cashtype, url) in
if image != nil{
// Do something Here after load image
}
print(error) // Your error is here
}

This is pod 'SDWebImage' = '4.0.0' and XCode == 11.3.1
self.userAvatar.sd_setImage(with: URL(string: __user_avatar), placeholderImage: UIImage(named: "UserDefalutAvatar"), options: .progressiveDownload, progress: { (receivedSize, expectedSize, targetURL) in
print("The progress value for the downloading is \(receivedSize)")
}) { (downloadedImage, downloadedError, imageCacheType, downloadedURL) in
if let _downloadedError = downloadedError {
print("Download image encountered error. \(_downloadedError.localizedDescription)")
return
}
if let _downloadedImage = downloadedImage {
print("This is downloaded image, you can do it what you want")
}
}

Related

Transparent background turns white

I'm uploading an image with a transparent background to firebase. The background becomes white when I downloaded it somehow. Any idea how to fix this?
Here's my putData function.
let uploadData = imageview.image!.jpegData(compressionQuality: 0.75)
let uploadTask = imagesRef.putData(uploadData!, metadata: nil, completion: { (metadata, error) in
guard let metadata = metadata else {
return
}
And here is the download function.
URLSession.shared.dataTask(with: NSURL(string: imageURL as! String)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print("ERROR LOADING IMAGES FROM URL: \(String(describing: error))")
DispatchQueue.main.async {
imageView.image = UIImage()
}
return
}
DispatchQueue.main.async {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: NSString(string: imageURL!))
imageView.image = downloadedImage
}
}
}).resume()
I'm uploading an image with a transparent background to firebase. The
background becomes white when I downloaded it somehow. Any idea how to
fix this?
You're doing it right, except you're getting getting a JPEG instead of PNG data ;) Remember that JPEG does not support transparent BG.
Hope it helps!

Image View Does not display image after setting image to it downloaded from url

here it is what i am doing.
extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}
i checked it in image its show image in preview in debug state. but after self.image = image
nothing change on image view image is not displaying. anyone know where the problem is ? thanks
Most efficient way for downloading image: SDWebImage
You can use a Cocoa Pod file for SDWebImage Library.
init the pod file.
pod 'SDWebImage'
install Pod file.
Swift snippet:
import SDWebImage
imageView.sd_setImage(with: URL(string: #"user-url"), placeholderImage: UIImage(named: "placeholder"))
Objective-C Snippet:
#import <SDWebImage/UIImageView+WebCache.h>
...
[imageView sd_setImageWithURL:[NSURL URLWithString:#"user-url"]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
Hope this will help to do efficient way to downloading images basically, whenever you are using UICollectionView or UITableView.
You can do it easier with Kingfisher library like below.
let url = URL(string: "url_of_your_image")
imageView.kf.setImage(with: url)
Try this below code
if data != nil { // check nil here
let image = UIImage(data: data)
DispatchQueue.main.async {
self.image = image
}
}
}.resume()
no need to check the image mime type, just a check of data and image is sufficient, since data will be nil for error and vice versa.
change as
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, let image = UIImage(data: data) else { return }
DispatchQueue.main.async {
self.image = image
}
}.resume()
And cross check with image with debugger.

How to show SDWebimage for background Colour in Swift

SDWebImage is great library for conversion URL to image. Its working great. But, I have following issue
// For imageview, we are setting like
eventImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "placeholder.png"))
// but, for Image as UIColor,
Showing compiler error if I use like following
self.scrollView.backgroundColor = UIColor(patternImage: sd_setImage(with: URL(string: imageUrl), placeholderImage:UIImage(named: "placeholder.png")))
even I tried like following
let imageBG = sd_setImage(with: URL(string: image), placeholderImage: UIImage(named: "placeholder.png"))
self.scrollView.backgroundColor = UIColor(patternImage: imageBG)
and its showing following error
Use of unresolved identifier 'sd_setImage'
Can anyone suggest me for how to fix this?
You need to use SDWebImageManager to download image at first and then use it in UIColor(patterImage:) constructor.
Try code below:
SDWebImageManager.shared().loadImage(with: URL(string: imageUrl), options: [], progress: nil, completed: { (image, data, error, cacheType, success, url) in
DispatchQueue.main.async {
guard let image = image else {
return
}
self.scrollView.backgroundColor = UIColor(patternImage: image)
}
})
That's happens because sd_setImage a UIImageView method (eventImageView should be a UIImageView), the init(patternImage:) declaration is:
init(patternImage image: UIImage)
which means that patternImage parameter is a UIImage not UIImageView.
To solve this, you should first load the image view, once the loading has done you could assign the color to be the image of the image view:
eventImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "placeholder.png")) { (image, error, cache, url) in
self.scrollView.backgroundColor = UIColor(patternImage: image)
}
Or (without the placeholder):
eventImageView.sd_setImage(with: URL(string: imageUrl)) { (image, error, cache, url) in
self.scrollView.backgroundColor = UIColor(patternImage: image)
}
sd_setImage(with is applicable to UIImageView
Create struct as -
struct ImageSetter {
static func setImage(scrollView: UIScrollView,myImageView: UIImageView,imageUrl: URL,placeHolderImage: UIImage){
myImageView.sd_setImage(with: imageUrl, placeholderImage: placeHolderImage, options: [], completed: { (imageBackground, error, cache, url) in
scrollView.backgroundColor = UIColor.init(patternImage: imageBackground ?? UIImage(named: "place-holder-image")!)
})
}
}
Use -
ImageSetter.setImage(scrollView: scrollView, myImageView: testImageView, imageUrl: url, placeHolderImage: image)
SDWebImage

How to set an image for a UIImageView from a URL inside a UITableViewCell?

I have the following code inside cellForRowAt method to fetch image and load into the cell's imageView. The image is confirmed to be downloaded from the print statements, but image is not showing in the cell. Please help me.
let request = URLRequest(url: URL(string: imageURL)!)
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest) {(data, response, error) in
// The download has finished.
if let e = error {
print("Error downloading picture: \(e)")
} else {
// No errors found.
// It would be weird if we didn't have a response, so check for that too.
if let res = response as? HTTPURLResponse {
print("Downloaded image with response code \(res.statusCode)")
if let imageData = data {
// Finally convert that Data into an image and do what you wish with it.
let image = UIImage(data: imageData)
// Do something with your image.
DispatchQueue.main.async {
cell.imgView.contentMode = .scaleAspectFit
cell.imgView.image = image
}
print("image received")
} else { print("Couldn't get image: Image is nil") }
} else { print("Couldn't get response code for some reason") }
}
}
dataTask.resume()
I think you just missing a reload of your cell's subviews (imageView) to show the image. You also probably want to keep track of the right cell.
cell.tag = indexPath.row //after you init the cell
DispatchQueue.main.async {
if cell.tag == indexPath.row
cell.imgView.contentMode = .scaleAspectFit
cell.imgView.image = image
cell.setNeedsLayout()
}

Using SdWebImage and Image Change immediately by swift

This is Question Video
I have a problem about imageView by using SDWebImage.
I change user's image and already get new user's image url, but when I push to this ViewController, it will show the old image first and change to new image.
What's wrong with me?
Thanks.
var avatar:String = "" // previous VC data pass to here
var photoImageView:UIImageView = { () -> UIImageView in
let ui = GeneratorImageView()
ui.backgroundColor = UIColor.clear
ui.layer.masksToBounds = true
ui.contentMode = .scaleAspectFill
return ui
}()
override func viewDidLoad() {
super.viewDidLoad()
iconImageFromUrl(imageView: iconImageView, url: avatar, isResize: false)
}
func iconImageFromUrl(imageView:UIImageView, url:String,isResize:Bool) {
imageView.setShowActivityIndicator(true)
imageView.setIndicatorStyle(.gray)
imageView.sd_setImage(with: URL(string:url), placeholderImage: nil, options: .lowPriority, progress: nil
, completed: { (image, error, cacheType, url) in
guard image != nil else{
imageView.image = resizeImage(image: #imageLiteral(resourceName: "defaultIcon"), newWidth: 50)
return
}
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
if data != nil
{
if let image = UIImage(data: data!)
{
DispatchQueue.main.async {
if isResize == true{
imageView.image = resizeImage(image: image, newWidth: 250)
}else{
imageView.image = image
}
}
}
}
}
})
}
sd_setImage method is written inside a category of UIImageView. After downloading the image it sets the image on UIImageview on its own and in the completion closure returns the downloaded/cached UIImage as well.
You dont need to create Data from imageUrl and set it again. If you want to resize image, you can do it on the returned image.
Also, you dont need to check the image nil for setting the default image, just pass the resized default image as placeholder image
imageView.sd_setImage(with: URL(string:url), placeholderImage: resizeImage(image: #imageLiteral(resourceName: "defaultIcon"), newWidth: 50), options: .lowPriority, progress: nil
, completed: { (image, error, cacheType, url) in
guard image != nil else {
return
}
if isResize {
imageView.image = resizeImage(image: image, newWidth: 250)
} })

Resources