在集合视图中 PNG 图像的透明背景

5
我有一个集合视图,显示可选的表情符号。目前这些表情符号是以灰色背景的形式显示给我看的,这个没什么问题,但是在图片文件中本身并没有这种灰色背景。
是否可能将这些表情符号图片的背景改为透明而不是灰色?

enter image description here

这是我的代码:

import UIKit

class EmojiPopup: UIView,UICollectionViewDataSource,UICollectionViewDelegate
{
    var collocationView : UICollectionView!
    var arrImagesList:NSMutableArray!
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        arrImagesList = NSMutableArray()
        self.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.1)
        let layout = UICollectionViewFlowLayout()
        //header gap
        layout.headerReferenceSize = CGSizeMake(30,30)
        //collection view item size
        layout.itemSize = CGSizeMake(75, 75)
        layout.minimumInteritemSpacing = 20
        layout.minimumLineSpacing = 10
        collocationView = UICollectionView(frame: CGRectMake(50,50,UIScreen.mainScreen().bounds.screenWidth - 100,UIScreen.mainScreen().bounds.screenHeight - 100), collectionViewLayout: layout)
        self.addSubview(collocationView)

        collocationView.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.001)
        collocationView.dataSource = self
        collocationView.delegate = self
        collocationView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier")

        let fm = NSFileManager.defaultManager()
        let path = NSBundle.mainBundle().resourcePath!
        let items = try! fm.contentsOfDirectoryAtPath(path)

        for item in items
        {
            if item.hasSuffix("png") && item.containsString("@") == false && item.containsString("AppIcon") == false && item.containsString("tick_blue") == false && item.containsString("video_camera") == false
            {
                arrImagesList.addObject(item)
            }
        }
    }
    var completeHandler:((String)->())?
    func showDetails(viewParent:UIView,doneButtonClick:((String)->())?)
    {
        completeHandler = doneButtonClick
        viewParent.addSubview(self)
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return arrImagesList.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let identifier="ImageCell\(indexPath.section)\(indexPath.row)"
        collectionView.registerClass(ImageViewCell.self, forCellWithReuseIdentifier:identifier)
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! ImageViewCell
        cell.backgroundColor = UIColor(white:1, alpha:0)
        cell.imgView.image = UIImage(named:arrImagesList[indexPath.row] as! String)
        cell.imgView.contentMode = .ScaleAspectFit
        return cell
    }  
//    func collectionView(collectionView: UICollectionView,
//        layout collectionViewLayout: UICollectionViewLayout,
//        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
//    {
//        let width=UIScreen.mainScreen().bounds.size.width-50
//        return CGSize(width:width/3, height:width/3)
//    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {
        //let cell=collectionView.cellForItemAtIndexPath(indexPath) as! ImageViewCell

        UIView.animateWithDuration(0.3, animations:{
            self.collocationView.alpha=0
            }, completion: { finished in

                if self.completeHandler != nil
                {
                    self.completeHandler!(self.arrImagesList[indexPath.row] as! String)
                }
                self.removeFromSuperview()
        })

    }
    func showDetails(viewParent:UIView,dictData : [String:String],index:Int,doneButtonClick:(()->())?,cancelBUttonClick:(()->())?)
    {
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

你尝试将集合视图的背景颜色设置为透明吗? - Ishmeet
@IshmeetSingh 这个设置似乎只影响图像之间的空间,这就是为什么它被设置为紫色的原因。我似乎无法删除/更改图像本身后面的灰色。它们是PNG格式,透明度应该显示正确?:( - Oscar
当您将图像添加到UIImageView时,通常默认会这样做。 - Ishmeet
可能你在Storyboard中给UIImageView分配了背景颜色。 - Ali Kıran
1个回答

3
cellForItemAtIndexPath 方法中。
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! ImageViewCell
cell.backgroundColor = UIColor(white:1, alpha:0)
cell.imgView.image = UIImage(named:arrImagesList[indexPath.row] as! String)
cell.imgView.backgroundColor = UIColor.clearColor()
cell.imgView.opaque = false
cell.imgView.contentMode = .ScaleAspectFit

return cell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接