带有白色背景的png透明图像元数据
png transparent image metadata coming with white background
元数据正在从 url 中提取 透明 png 图像。但是,UIImageView 不是 透明的并且有白色背景。
是否有解决此问题的方法?
也许.pngData()
@IBOutlet weak var mainCenterIcon: UIImageView!
func initAudioPlayer() {
let url = URL(string: activeAudioURL)
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
//MetaData to recieve album artwork
let metadataList = playerItem.asset.metadata
for item in metadataList {
guard let key = item.commonKey?.rawValue, let value = item.value else{
continue
}
switch key {
case "artwork" where value is Data : mainCenterIcon.image = UIImage(data: value as! Data)
default:
continue
}
}
}
您必须将图像背景设置为透明颜色并将不透明标志设置为 false。
使用这两个:
imageView.backgroudColor = UIColor.clear
imageView.isOpaque = false
我找到了解决办法:
extension UIImage {
func imageByMakingWhiteBackgroundTransparent() -> UIImage? {
let image = UIImage(data: self.jpegData(compressionQuality: 1.0)!)!
let rawImageRef: CGImage = image.cgImage!
let colorMasking: [CGFloat] = [222, 255, 222, 255, 222, 255]
UIGraphicsBeginImageContext(image.size);
let maskedImageRef = rawImageRef.copy(maskingColorComponents: colorMasking)
UIGraphicsGetCurrentContext()?.translateBy(x: 0.0,y: image.size.height)
UIGraphicsGetCurrentContext()?.scaleBy(x: 1.0, y: -1.0)
UIGraphicsGetCurrentContext()?.draw(maskedImageRef!, in: CGRect.init(x: 0, y: 0, width: image.size.width, height: image.size.height))
let result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result
}
}
调用函数:
mainCenterIcon.image = UIImage(data: value as! Data)
mainCenterIcon.image = mainCenterIcon.image!.imageByMakingWhiteBackgroundTransparent()
参考:Original Post
元数据正在从 url 中提取 透明 png 图像。但是,UIImageView 不是 透明的并且有白色背景。
是否有解决此问题的方法?
也许.pngData()
@IBOutlet weak var mainCenterIcon: UIImageView!
func initAudioPlayer() {
let url = URL(string: activeAudioURL)
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
//MetaData to recieve album artwork
let metadataList = playerItem.asset.metadata
for item in metadataList {
guard let key = item.commonKey?.rawValue, let value = item.value else{
continue
}
switch key {
case "artwork" where value is Data : mainCenterIcon.image = UIImage(data: value as! Data)
default:
continue
}
}
}
您必须将图像背景设置为透明颜色并将不透明标志设置为 false。
使用这两个:
imageView.backgroudColor = UIColor.clear
imageView.isOpaque = false
我找到了解决办法:
extension UIImage {
func imageByMakingWhiteBackgroundTransparent() -> UIImage? {
let image = UIImage(data: self.jpegData(compressionQuality: 1.0)!)!
let rawImageRef: CGImage = image.cgImage!
let colorMasking: [CGFloat] = [222, 255, 222, 255, 222, 255]
UIGraphicsBeginImageContext(image.size);
let maskedImageRef = rawImageRef.copy(maskingColorComponents: colorMasking)
UIGraphicsGetCurrentContext()?.translateBy(x: 0.0,y: image.size.height)
UIGraphicsGetCurrentContext()?.scaleBy(x: 1.0, y: -1.0)
UIGraphicsGetCurrentContext()?.draw(maskedImageRef!, in: CGRect.init(x: 0, y: 0, width: image.size.width, height: image.size.height))
let result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result
}
}
调用函数:
mainCenterIcon.image = UIImage(data: value as! Data)
mainCenterIcon.image = mainCenterIcon.image!.imageByMakingWhiteBackgroundTransparent()
参考:Original Post