gpt4 book ai didi

swift - 如何使用Core Graphics编辑高分辨率图像?

转载 作者:行者123 更新时间:2023-11-30 10:04:05 26 4
gpt4 key购买 nike

我正在尝试在高分辨率图像上绘制一条路径,这对于 iPhone 来说并不复杂,但如果我向路径添加阴影,一切都会滞后。仅当我处理具有特定分辨率 (2000 x 3000) 的图像时,它才会滞后。

Storyboard View 是:

-Scroll View
-Image View
-Draw View

所以当我需要绘制时,我将 DrawingView 放在 ImageView 之上。
因此 ImageView 和 DrawView (view.bounds.size) 具有与图像相同的分辨率(例如 2000 x 3000)(有问题)。
我正在以高分辨率绘制 View 。

不直接调用drawRect:,而是仅在touchesBegan()内调用setNeedsDisplay()并且在执行一些操作(path.moveToPointpath.addCurveToPoint、数组操作)并将点添加到我的数组后,touchesMoved()

drawRect:中我基本上有:

override func drawRect(rect: CGRect) {
print(self.bounds.size)
UIColor.greenColor().setStroke()
path.lineCapStyle = .Round
path.lineJoinStyle = .Round
path.lineWidth = 60.0
context = UIGraphicsGetCurrentContext()!
CGContextAddPath(context, path.CGPath)
CGContextSetShadowWithColor(context, CGSizeZero, 14.0, UIColor.whiteColor().CGColor) // <-- with this shadow it lags a lot.
path.stroke()
}

我的路径是UIBezierPath()

有什么提高速度的想法吗?

更新:我按照@brimstone 所说的进行操作。我现在拥有分辨率较低的 ImageView,但必须将绘制的路径应用于高分辨率图像。
(我正在尝试使用用户绘制的路径手动裁剪图像)
在这段代码中我已经得到了我的闭合路径:

    let layer = CAShapeLayer()
layer.path = path.CGPath
self.imageToEditView.layer.mask = layer
UIGraphicsBeginImageContext(self.imageEdited.size)
self.imageToEditView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let croppedCGImage = CGImageCreateWithImageInRect(image.CGImage!, CGPathGetBoundingBox(path.CGPath))
let croppedImage = UIImage(CGImage: croppedCGImage!)
self.imageToEditView.image = croppedImage
self.imageToEditView.layer.mask = nil

imageToEditView.bounds.size = 低分辨率
imageEdited.size = 高分辨率

当我 renderInContext 时,我需要设置高分辨率(我认为)。但是现在如何更改 imageView 的分辨率呢?

最佳答案

尝试缩小其尺寸以便用户进行绘制(在小 iPhone 屏幕上不会对用户体验产生巨大影响),然后将编辑应用到高分辨率图像。

要缩小图像大小,请使用 UIImagePNGRepresentation,这可能会使您的图像足够小,或者(如果您仍然遇到内存问题),请尝试使用 this tutorial 中的技术。和 this answer使其变得更小。

然后,您可以获取他们绘制的内容并将其应用到高分辨率图像。

或者,看看 Apple 的高分辨率优化技术:https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/SupportingHiResScreensInViews/SupportingHiResScreensInViews.html

关于swift - 如何使用Core Graphics编辑高分辨率图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37034367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com