5

This is the code that I am using to set the user image which works great to actually get and set the image. I get a white outline to the picture. Any idea how to make this transparent?

let processor = RoundCornerImageProcessor(cornerRadius: 50)
self.userPhoto.backgroundColor = Colors.accent
self.userPhoto.kf.setImage(with: url, placeholder: UIImage(named: "ic_camera_alt_48pt"), options: [.processor(processor)])

Example with 50 enter image description here

ajonp
  • 111
  • 10

2 Answers2

6

The format of the original image doesn't support transparency (i.e. JPEG). You must convert the image to one that does (i.e. PNG) prior to caching.

From the Kingfisher Cheat Sheet:

By default (DefaultCacheSerializer), Kingfisher will respect the input image format and try to keep it unchanged. However, there are some exceptions. A common case is that when you using a RoundCornerImageProcessor, you may always want the alpha channel. If your original image is jpg, you may want to set the png serializer instead:

let roundCorner = RoundCornerImageProcessor(cornerRadius: 20)
imageView.kf.setImage(with: url, 
    options: [.processor(roundCorner), 
              .cacheSerializer(FormatIndicatedCacheSerializer.png)]
)
Brandon Zacharie
  • 2,320
  • 2
  • 24
  • 29
  • This is accepted answer, and just an addition, you may have to use clipToBounds=true on your UIImageView if you're using ResizingImageProcessor along with RoundCornerImageProcessor. – bra.Scene Jul 02 '20 at 14:55
2

You don't need the RoundCornerImageProcessor

self.userPhoto.layerCornerRadius = 50
self.userPhoto.clipsToBounds = true
self.userPhoto.backgroundColor = Colors.accent
self.userPhoto.kf.setImage(with: url, placeholder: UIImage(named: "ic_camera_alt_48pt"))
Tieme
  • 62,602
  • 20
  • 102
  • 156