Swapnil Dhanwal Swapnil Dhanwal - 6 months ago 37
Swift Question

Shadow not appearing under UIImageView (Swift)

I have written the following code to download and display an image in the cells of a tableView. However, the drop shadow isn't appearing at all and I can't figure out what is wrong.

cell.societyImage.setIndicatorStyle(UIActivityIndicatorViewStyle.White)
cell.societyImage.setShowActivityIndicatorView(true)
cell.societyImage.sd_setImageWithURL(NSURL(string: pictureURLs[objectIds[indexPath.row]]!),completed: { (image, error, cache, url) in
cell.societyImage.layer.shadowColor = UIColor(white: 0.7, alpha: 0.7).CGColor
cell.societyImage.layer.shadowOffset = CGSizeMake(3, 3)
cell.societyImage.layer.shadowOpacity = 0.4
cell.societyImage.layer.masksToBounds = true
})


Any help is much appreciated!

Answer

About maskToBounds: if you put it true then you won't see shadow as it clips sublayer where else in false it allow to extent layer.

...    
cell.societyImage.sd_setImageWithURL(NSURL(string: pictureURLs[objectIds[indexPath.row]]!),completed: { (image, error, cache, url) in     
    dispatch_async(dispatch_get_main_queue()) {
         let shadowPath = UIBezierPath(rect: cell.societyImage.bounds).CGPath
         cell.societyImage.layer.shadowColor = UIColor(white: 0.7, alpha: 0.7).CGColor
         cell.societyImage.layer.shadowOffset = CGSizeMake(3, 3)
         cell.societyImage.layer.shadowOpacity = 0.4
         cell.societyImage.layer.masksToBounds = false
         cell.societyImage.layer.shadowPath = shadowPath.CGPath
         cell.societyImage.layer.radius = 2
    })   
})