swift - 如何在图像周围删除 UIGraphicsGetCurrentContext 而不是填充清除或更改上下文的大小 - Swift 4
问题描述
UIGraphicsBeginImageContextWithOptions(startingImageL.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()!
startingImageL.draw(in: CGRect(origin: CGPoint.zero, size: startingImageL.size), blendMode: .copy, alpha: 1.0)
context.setBlendMode(.copy)
context.setFillColor(UIColor.clear.cgColor)
let rectPath = UIBezierPath(rect: CGRect(origin: CGPoint.zero, size: startingImageL.size))
let cropRectPath = UIBezierPath(rect: cropZone)
print("cropRectPath \(cropRectPath.debugDescription) ")
print("cropZone \(cropZone.debugDescription) ")
print("cropZone width \(cropZone.width) ")
print("cropZone height \(cropZone.height) ")
print("cropZone maxX \(cropZone.maxX) ")
print("cropZone maxY \(cropZone.maxY) ")
rectPath.append(cropRectPath)
rectPath.usesEvenOddFillRule = true
context.saveGState()
rectPath.fill()
context.clip(to: cropZone)
// UIRectClip(cropZone) <-nope
// UIRectFrame(cropZone) <- nope
// rectPath.addClip() <nope
图像前
它的效果很好,但我希望它删除它周围的所有空间而不是填充(粉红色),然后图像会填满视图。谢谢!
解决方案
根据马特的建议,我所需要的只是推到负坐标并从那里开始图像!!所以我所需要的(一旦你有了裁剪矩形)就是这样:
UIGraphicsBeginImageContextWithOptions(cropZone.size, false, 0.0)
UIGraphicsGetCurrentContext()
startingImageL.draw(at: CGPoint(x:-cropZone.origin.x,
y:-cropZone.origin.y))
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
谢谢你
推荐阅读
- python - Python - 从文件文件末尾删除最后 2 个字符
- python - Python joblib.Memory - 强制重新计算函数值和/或判断函数值是否来自缓存的方法
- html - 如何将文字放在图像的底部?
- python - Dataframe:添加两个 Daframe 的值
- android - 在 ubuntu 中安装没有 SDK 管理器的 Android NDK
- windows - 为什么 RegisterHotKey 使用 Ctrl-C 会成功?
- assembly - 皮质 r52 架构上的 vstm 是否需要对齐?
- javascript - 使用 jQuery 查找元素并在其中附加值
- node.js - 如何使用 REST API 将数据发送到 websocket 服务器?
- google-cloud-platform - 如何在 VPN 后面的云上托管我的 localhost:8080 应用程序?