I have a UITableViewCell which an UIImageView in it, the image placeholder works very fine but when use SDWebImage to get image from url with an actual data, the image will display outside the UIImageView.
I have tried using below but none worked.
cell.imageView?.contentMode = .scaleAspectFit
cell.imageView?.clipsToBounds = false
cell.imageView?.layer.masksToBounds = true
I have attached reference image to further understanding.
UITableViewCell
I have also tried resizing the image from server to 40 x 40, but still same issue.
//cell.imageView?.sd_setImage(with: URL(string: "\(Constants.string.WEB)/assets/\(dict.image)"), placeholderImage: UIImage(named: Constants.string.IMAGE_PLACEHOLDER_60))
cell.imageView?.sd_setImage(with: URL(string: "\(Constants.string.WEB)/assets/\(dict.image)")) { (image, error, cache, urls) in
if error != nil {
cell.imageView?.image = UIImage(named: Constants.string.IMAGE_PLACEHOLDER_60)!.resizeImageWith(newSize: CGSize(width: 40, height: 40))
} else {
cell.imageView?.image = image!.resizeImageWith(newSize: CGSize(width: 40, height: 40))
}
}
What is the size of the imageView? Did you set any constraints? Also set cell.imageView?.clipsToBounds to true. Ensure that the imageView has height and width constraint set and also leading and top constraints set.
Related
I was wondering how to create a UIImageView and set it’s image property and show it on the live view. This is Swift Playgrounds for iPad so there is no storyboards. I can’t add in a UIImageView unless I code it. How do I create a UIImageView in Swift Playgrounds for iPad?
let image = UIImage(named: “photo.png”)
let imageView = UIImageView()
imageView.image = image
view.addSubview(imageView)
imageView.Frame = CGRect(x: 0, y: 0, width: int, height: int)
This creates a custom frame. To have the frame match the image use imageView.image = UIImage(named: image) This is based of of rmaddy’s comment below
I am having a tableview cell containing an ImageView, two labels. I need to adjust the size of image view in accordance with the image coming from server and then adjust the labels.
Please refer to the cell screen shot.
I am downloading image using
cell.imageViewKnowledgeFeed.sd_setImage(with: URL(string: urlStr as String), completed: { (image, error,imageCacheType , nil) in
cell.imageViewKnowledgeFeed.image = image
print(image?.size)
cell.imageViewKnowledgeFeed.frame = CGRect(x: cell.imageViewKnowledgeFeed.frame.origin.x,
y: 100,
width: cell.imageViewKnowledgeFeed.frame.size.width,
height: cell.imageViewKnowledgeFeed.frame.size.height)
// tableView.reloadRows(at: [indexPath], with: .none)
})
Constraints are as follows
I am using content mode AspectFill into UIImageView.
You need to create outlets for constraints and they set them programmatically in accordance with size of the image and afterwards you can call layoutSubviews() to adjust other labels and associated designs.
How can I add UIViewContentMode.center to this UIImageView while also keeping .scaleAspectFill?
func insertImage() {
let theImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 300))
theImageView.image = #imageLiteral(resourceName: "coolimage")
theImageView.contentMode = UIViewContentMode.scaleAspectFill
view.addSubview(theImageView)
}
Furthermore, can somebody explain to me what the "view" exactly is in the last line "view.addSubview(theImageView)"? Is it the mysterious "view hierarchy" that I read about? Why can't I simply initialize the UIImageView? Why must it be bound to something called "view" that I haven't explicitly created? There is only a UIViewController and a UIImageView so far.
As far as I know, you can't set content mode to both aspect fit and center. However, center will do what aspect fit does providing the image size is smaller than the size of the imageView. If not, use aspect fit. The following code ought to allow you to differentiate between the two:
if (theImageView.bounds.size.width > UIImage(named: "coolimage")?.size.width && theImageView.bounds.size.height > UIImage(named: "coolimage")?.size.height) {
theImageView.contentMode = .aspectFit
} else {
theImageView.contentMode = .center
}
As for the second part of your question, I'll refer you to this thread, which has a fairly comprehensive explanation of UIViewController vs UIView. Hope that helps.
I'm using SDWebImage for my Swift. I can't figure out why (frankly, HOW) it's not working. there is no image in imageview.
so this is what i'm trying. parse query PFFile(s) (url converted) stored in a var urlArray:[URL] = []
i have a UIScrollView with UIImageView inside.
in short: query parse - download PFFile URLs to an array (append).
for index in 0..<self.urlArray.count {
var frame = CGRect.zero
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
self.scrollView.isPagingEnabled = true
let subView = UIImageView(frame: frame)
subView.sd_setImage(with: self.urlArray[index], placeholderImage: #imageLiteral(resourceName: "profile"))
//subView.contentMode = .scaleAspectFit
self.scrollView.addSubview(subView)
}
first url image appears ok. then no pagination & no error (array numbers OK) just nothing apears but first pic. used this : imageview.sd_setImage(with: self.urlArray[index]) is this wrong approach?
thanks
I don't see the code where you update the contentSize of the UIScrollView. Unless you do that, the scroll view won't scroll for you. The additional image views may be there, but hidden outside of the scroll view's current bounds.
sdWebImage is a library using for image caching.
If you wanna detect error for downloading image by sdWebImage then you apply below code and track response from server.
cell.imageView.sd_setImageWithURL(NSURL (string: sendImgUri!), placeholderImage: UIImage (named: "pro.png"), options: .CacheMemoryOnly, progress: { (i, m) in
}, completed: { (image, error, chachType, url) in
if(error != nil)
{
print(error)
}
else if(image != nil)
{
print(image)
}
})
I have a method to change the colour of an image. I use this colour icons in menus of my app quickly without having to go any create and image of the correct colour each time.
/// Tint an image with a selected colour.
///
/// - Parameters:
/// - image: The image you wish to colour
/// - imageView: The imageView containing the image
/// - colour: Colour you wish to tint image with. Set as nil if you dont want to change it or im image is multicoloured.
func tint(icon image:UIImage, for imageView:UIImageView, with colour:UIColor?) {
if colour != nil {
let template = image.withRenderingMode(.alwaysTemplate)
imageView.tintColor = colour!
imageView.image = template
} else {
imageView.image = image
}
}
This works fine most of the time. However my problem is trying to set the colour of an image in a navigation bar it doesn't work at all and the image stays its original colour.
I'm trying the following
let dashboardButton = UIButton(type: .custom)
let dashboardButtonImage = UIImage(named: "example image name")
dashboardButton.setImage(dashboardButtonImage.imageResize(sizeChange: CGSize(width: 20, height: 20)), for: .normal)
style.tint(icon: dashboardButtonImage, for: dashboardButton.imageView!, with: .red)
dashboardButton.contentHorizontalAlignment = .fill
dashboardButton.contentVerticalAlignment = .fill
dashboardButton.imageView?.contentMode = .scaleAspectFit
dashboardButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
dashboardButton.addTarget(self, action: #selector(ExampleViewController.goToDashboard), for: .touchUpInside)
let dashboardItem = UIBarButtonItem(customView: dashboardButton)
self.navigationItem.setRightBarButtonItems([dashboardItem], animated: false)
I could very easily fix the problem by just making a graphic of the correct colour. However I want to keep these colour changes in app for when a client decides the want to change colours. I'm wondering why I cant get it to work in the nav bar? Is there a fix for this?
There is always a problem accessing a buttons image property of its image view directly like this:
buttom.imageView.image = testImage
You could try this instead which works for me:
func tint(icon image: UIImage, for button: UIButton, with colour: UIColor?) {
if colour != nil {
let template = image.withRenderingMode(.alwaysTemplate)
button.setImage(template, for: .normal)
button.imageView!.tintColor = .red
} else {
button.setImage(image, for: .normal)
}
}
Using that you pass the button instead of the image view and the function uses the setImage method of UIButton to set it. That appears to get round the problem.