iOS 简单绘图之图片擦除

iOS 简单绘图之图片擦除

图片擦除场景

适用于解谜类,图片擦除得到图片下东西。或者绘图类,图片擦除,只展示图片其中一部分。

方式一

通过clip方式,将想要展示的图片区域给展示出来。

方式二

通过上下文的clear,让图片部分区域擦除掉。

		let imageView = UIImageView(frame: CGRectMake(10, 350, 400, 200))
        view.addSubview(imageView)
        let image0 = UIImage(named: "9")!
        
        let render = UIGraphicsImageRenderer(size: CGSizeMake(400, 200))
        let timage = render.image { context in
            let cgContext = context.cgContext
            image0.draw(in: CGRectMake(0, 0, 400, 200))
            cgContext.clear(CGRectMake(50, 50, 100, 40)) //在图片上可用
        }
        imageView.image = timage

通过这种方式,可以实现根据手指滑动,清除图片内容。

方式三

通过blendMode实现图片擦除,当然,其他blendMode能够实现更多更加复杂的效果,具体可参考官方文档。

let imageView = UIImageView(frame: CGRectMake(10, 350, 400, 200))
        view.addSubview(imageView)
        let image0 = UIImage(named: "9")!
        
        let blendModes: [CGBlendMode] = [.clear, .color, .colorBurn, .colorDodge, .copy, .darken, .destinationAtop, .destinationIn, .destinationOut, .destinationOver, .difference, .exclusion, .hardLight, .hue, .lighten, .luminosity, .multiply, .normal, .overlay, .plusDarker, .plusLighter, .saturation, .screen, .softLight, .sourceAtop, .sourceIn, .sourceOut, .xor] //28种,根据业务需求处理不同业务逻辑
        let w1 = imageView.size.width / CGFloat(blendModes.count)
        
        let render = UIGraphicsImageRenderer(size: CGSizeMake(400, 200))
        let timage = render.image { context in
            let cgContext = context.cgContext
            image0.draw(in: CGRectMake(0, 0, 400, 200))
            cgContext.clear(CGRectMake(50, 50, 100, 40)) //在图片上可用
            
            UIColor.red.setFill()
            for i in 0..<blendModes.count {
                cgContext.setBlendMode(blendModes[i])
                cgContext.fill([CGRectMake(w1*CGFloat(i), 100, w1, 20)])
            }
        }
        imageView.image = timage
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xinxin_0

带我喝杯茶~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值