如何设置圆形图像的背景颜色?

时间:2016-11-09 20:44:44

标签: ios swift

我有一个图像,点击一个按钮,它的alpha会随着它的黑暗而改变。

我想到这样做的最好方法如下:

cell.followUserImage.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)

以下是更改前的图片:

enter image description here

以下是以下图片:

enter image description here

正如你所看到的,它只会使角落变暗。我希望它只是使图像变暗并改变其alpha分量。

我将如何做到这一点?

2 个答案:

答案 0 :(得分:1)

来自@Rob的好主意

以下是 Swift 3 中的代码:

let mask = UIImageView(frame: imageView.bounds)
mask.image = imageView.image
mask.contentMode = imageView.contentMode
imageView.mask = mask

let overlay = UIView(frame: imageView.bounds)
overlay.backgroundColor = UIColor(white: 0, alpha: 0.2)
imageView.addSubview(overlay)

enter image description here

要使整个imageView半透明,您可以设置它的alpha:

imageView.alpha = 0.5     

答案 1 :(得分:1)

如果要创建一个暗淡的图像,屏蔽该原始图像,您可以执行以下操作(在Swift 3中):

with
     test_data ( dt, item ) as (
       select 1, 'A' from dual union all
       select 1, 'B' from dual union all
       select 2, 'C' from dual union all
       select 3, 'A' from dual
     )
-- end of test data; solution (SQL query) begins below this line
select dt, count(*) as ct
from   ( select   min(dt) as dt, item
         from     test_data
         group by item
       )
group by dt
;

        DT         CT
---------- ----------
         1          2
         2          1

其中

with
     test_data ( dt, item ) as (
       select 1, 'A' from dual union all
       select 1, 'B' from dual union all
       select 2, 'C' from dual union all
       select 3, 'A' from dual
     )
-- end of test data; solution (SQL query) begins below this line
select dt, count(case when ord = 1 then 'x' end) as ct
from   (
         select dt, item, count(*) over (partition by item order by dt) ord
         from   test_data
       )
group by dt
;

        DT         CT
---------- ----------
         1          2
         2          1
         3          0

或者,在Swift 2中:

imageView.image = image.darkened()

产生(在暗淡的再现旁边显示图像):

enter image description here