绘图类绘制直线而不是曲线

2023-11-24

我有下面的代码,使用 UIBezierPath 绘制线条。

代码使用addCurveToPoint它应该使用三次贝塞尔路径绘制曲线,但是代码的最终结果是绘制连接的直线,但是addLineToPoint没有被使用。

可能发生了什么,为什么代码不绘制曲线?

enter image description here

import UIKit

class DrawingView: UIView, UITextFieldDelegate {

// Modifiable values within the code
let lineWidth : CGFloat = 2.0
let lineColor = UIColor.redColor()
let lineColorAlpha : CGFloat = 0.4
let shouldAllowUserChangeLineWidth = true
let maximumUndoRedoChances = 10

var path = UIBezierPath()

var previousImages : [UIImage] = [UIImage]()

// Represents current image index
var currentImageIndex = 0

// Control points for drawing curve smoothly
private var controlPoint1 : CGPoint?
private var controlPoint2 : CGPoint?

private var undoButton : UIButton!
private var redoButton : UIButton!

private var textField : UITextField!

//MARK: Init methods
override init(frame: CGRect) {
    super.init(frame: frame)
    setDefaultValues()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setDefaultValues()
}

// Draw the path when needed
override func drawRect(rect: CGRect) {
    if currentImageIndex > 0 {
        previousImages[currentImageIndex - 1].drawInRect(rect)
    }

    lineColor.setStroke()
    path.strokeWithBlendMode(CGBlendMode.Normal, alpha: lineColorAlpha)
}

override func layoutSubviews() {
    super.layoutSubviews()

    redoButton.frame = CGRectMake(bounds.size.width - 58, 30, 50, 44)
    if shouldAllowUserChangeLineWidth {
        textField.center = CGPointMake(center.x, 52)
    }
}

func setDefaultValues() {
    multipleTouchEnabled = false
    backgroundColor = UIColor.whiteColor()
    path.lineWidth = lineWidth

    addButtonsAndField()
}

func addButtonsAndField() {
    undoButton = UIButton(frame: CGRectMake(8, 30, 50, 44))
    undoButton.setTitle("Undo", forState: UIControlState.Normal)
    undoButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    undoButton.backgroundColor = UIColor.lightGrayColor()
    undoButton.addTarget(self, action: "undoButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
    addSubview(undoButton)

    redoButton = UIButton(frame: CGRectMake(bounds.size.width - 58, 30, 50, 44))
    redoButton.setTitle("Redo", forState: UIControlState.Normal)
    redoButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    redoButton.backgroundColor = UIColor.lightGrayColor()
    redoButton.addTarget(self, action: "redoButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
    addSubview(redoButton)

    if shouldAllowUserChangeLineWidth {
        textField = UITextField(frame: CGRectMake(0, 0, 50, 40))
        textField.backgroundColor = UIColor.lightGrayColor()
        textField.center = CGPointMake(center.x, 52)
        textField.keyboardType = UIKeyboardType.NumberPad
        textField.delegate = self
        addSubview(textField)
    }
}

//MARK: Touches methods
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    // Find the start point and move the path there
    endEditing(true)

    let touchPoint = touches.first?.locationInView(self)

    path.moveToPoint(touchPoint!)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touchPoint = touches.first?.locationInView(self)
    controlPoint1 = CGPointMake((path.currentPoint.x + touchPoint!.x) / 2, (path.currentPoint.y + touchPoint!.y) / 2)
    controlPoint2 = CGPointMake((path.currentPoint.x + touchPoint!.x) / 2, (path.currentPoint.y + touchPoint!.y) / 2)

    path.addCurveToPoint(touchPoint!, controlPoint1: controlPoint1!, controlPoint2: controlPoint2!)
    setNeedsDisplay()
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touchPoint = touches.first?.locationInView(self)
    controlPoint1 = CGPointMake((path.currentPoint.x + touchPoint!.x) / 2, (path.currentPoint.y + touchPoint!.y) / 2)
    controlPoint2 = CGPointMake((path.currentPoint.x + touchPoint!.x) / 2, (path.currentPoint.y + touchPoint!.y) / 2)

    path.addCurveToPoint(touchPoint!, controlPoint1: controlPoint1!, controlPoint2: controlPoint2!)

    savePreviousImage()
    setNeedsDisplay()

    // Remove all points to optimize the drawing speed
    path.removeAllPoints()
}

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    touchesEnded(touches!, withEvent: event)
}

//MARK: Selector methods
func undoButtonTapped(sender : UIButton) {
    if currentImageIndex > 0 {

        setNeedsDisplay()

        currentImageIndex--
    }
}

func redoButtonTapped(sender : UIButton) {
    if currentImageIndex != previousImages.count {

        setNeedsDisplay()

        currentImageIndex++
    }
}

//MARK: UITextFieldDelegate
func textFieldDidEndEditing(textField: UITextField) {
    if let n = NSNumberFormatter().numberFromString(textField.text!) {
        if n.integerValue > 0 {
            path.lineWidth = CGFloat(n)
        }
    }
}

//MARK: Saving images for reloading when undo or redo called
private func savePreviousImage() {
    UIGraphicsBeginImageContextWithOptions(bounds.size, true, UIScreen.mainScreen().scale)
    lineColor.setStroke()

    // Create a image with white color
    let rectPath = UIBezierPath(rect: bounds)
    backgroundColor?.setFill()
    rectPath.fill()

    if currentImageIndex > 0 {
        previousImages[currentImageIndex - 1].drawInRect(bounds)
    }

    path.strokeWithBlendMode(CGBlendMode.Normal, alpha: lineColorAlpha)

    if previousImages.count >= currentImageIndex {
        previousImages.removeRange(currentImageIndex..<previousImages.count)
    }

    if previousImages.count >= maximumUndoRedoChances {
        previousImages.removeFirst()
    }
    else {
        currentImageIndex++
    }

    previousImages.append(UIGraphicsGetImageFromCurrentImageContext())
    UIGraphicsEndImageContext()
}
}

有几个问题:

  1. 您使用的控制点是两点之间的中点,从而产生线段。您可能想要选择平滑曲线的控制点。看http://spin.atomicobject.com/2014/05/28/ios-interpolating-points/.

    下面是一个简单平滑算法的 Swift 3 实现,以及上述 Hermite 和 Catmull-Rom 样条方法的 Swift 版本:

    extension UIBezierPath {
    
        /// Simple smoothing algorithm
        ///
        /// This iterates through the points in the array, drawing cubic bezier
        /// from the first to the fourth points, using the second and third as
        /// control points.
        ///
        /// This takes every third point and moves it so that it is exactly inbetween
        /// the points before and after it, which ensures that there is no discontinuity
        /// in the first derivative as you join these cubic beziers together.
        ///
        /// Note, if, at the end, there are not enough points for a cubic bezier, it
        /// will perform a quadratic bezier, or if not enough points for that, a line.
        ///
        /// - parameter points: The array of `CGPoint`.
    
        convenience init?(simpleSmooth points: [CGPoint]) {
            guard points.count > 1 else { return nil }
    
            self.init()
    
            move(to: points[0])
    
            var index = 0
    
            while index < (points.count - 1) {
                switch (points.count - index) {
                case 2:
                    index += 1
                    addLine(to: points[index])
                case 3:
                    index += 2
                    addQuadCurve(to: points[index], controlPoint: points[index-1])
                case 4:
                    index += 3
                    addCurve(to: points[index], controlPoint1: points[index-2], controlPoint2: points[index-1])
                default:
                    index += 3
                    let point = CGPoint(x: (points[index-1].x + points[index+1].x) / 2,
                                        y: (points[index-1].y + points[index+1].y) / 2)
                    addCurve(to: point, controlPoint1: points[index-2], controlPoint2: points[index-1])
                }
            }
        }
    
        /// Create smooth UIBezierPath using Hermite Spline
        ///
        /// This requires at least two points.
        ///
        /// Adapted from https://github.com/jnfisher/ios-curve-interpolation
        /// See http://spin.atomicobject.com/2014/05/28/ios-interpolating-points/
        ///
        /// - parameter hermiteInterpolatedPoints: The array of CGPoint values.
        /// - parameter closed:                    Whether the path should be closed or not
        ///
        /// - returns:  An initialized `UIBezierPath`, or `nil` if an object could not be created for some reason (e.g. not enough points).
    
        convenience init?(hermiteInterpolatedPoints points: [CGPoint], closed: Bool) {
            self.init()
    
            guard points.count > 1 else { return nil }
    
            let numberOfCurves = closed ? points.count : points.count - 1
    
            var previousPoint: CGPoint? = closed ? points.last : nil
            var currentPoint:  CGPoint  = points[0]
            var nextPoint:     CGPoint? = points[1]
    
            move(to: currentPoint)
    
            for index in 0 ..< numberOfCurves {
                let endPt = nextPoint!
    
                var mx: CGFloat
                var my: CGFloat
    
                if previousPoint != nil {
                    mx = (nextPoint!.x - currentPoint.x) * 0.5 + (currentPoint.x - previousPoint!.x)*0.5
                    my = (nextPoint!.y - currentPoint.y) * 0.5 + (currentPoint.y - previousPoint!.y)*0.5
                } else {
                    mx = (nextPoint!.x - currentPoint.x) * 0.5
                    my = (nextPoint!.y - currentPoint.y) * 0.5
                }
    
                let ctrlPt1 = CGPoint(x: currentPoint.x + mx / 3.0, y: currentPoint.y + my / 3.0)
    
                previousPoint = currentPoint
                currentPoint = nextPoint!
                let nextIndex = index + 2
                if closed {
                    nextPoint = points[nextIndex % points.count]
                } else {
                    nextPoint = nextIndex < points.count ? points[nextIndex % points.count] : nil
                }
    
                if nextPoint != nil {
                    mx = (nextPoint!.x - currentPoint.x) * 0.5 + (currentPoint.x - previousPoint!.x) * 0.5
                    my = (nextPoint!.y - currentPoint.y) * 0.5 + (currentPoint.y - previousPoint!.y) * 0.5
                }
                else {
                    mx = (currentPoint.x - previousPoint!.x) * 0.5
                    my = (currentPoint.y - previousPoint!.y) * 0.5
                }
    
                let ctrlPt2 = CGPoint(x: currentPoint.x - mx / 3.0, y: currentPoint.y - my / 3.0)
    
                addCurve(to: endPt, controlPoint1: ctrlPt1, controlPoint2: ctrlPt2)
            }
    
            if closed { close() }
        }
    
        /// Create smooth UIBezierPath using Catmull-Rom Splines
        ///
        /// This requires at least four points.
        ///
        /// Adapted from https://github.com/jnfisher/ios-curve-interpolation
        /// See http://spin.atomicobject.com/2014/05/28/ios-interpolating-points/
        ///
        /// - parameter catmullRomInterpolatedPoints: The array of CGPoint values.
        /// - parameter closed:                       Whether the path should be closed or not
        /// - parameter alpha:                        The alpha factor to be applied to Catmull-Rom spline.
        ///
        /// - returns:  An initialized `UIBezierPath`, or `nil` if an object could not be created for some reason (e.g. not enough points).
    
        convenience init?(catmullRomInterpolatedPoints points: [CGPoint], closed: Bool, alpha: CGFloat) {
            self.init()
    
            guard points.count > 3 else { return nil }
    
            assert(alpha >= 0 && alpha <= 1.0, "Alpha must be between 0 and 1")
    
            let endIndex = closed ? points.count : points.count - 2
    
            let startIndex = closed ? 0 : 1
    
            let kEPSILON: CGFloat = 1.0e-5
    
            move(to: points[startIndex])
    
            for index in startIndex ..< endIndex {
                let nextIndex = (index + 1) % points.count
                let nextNextIndex = (nextIndex + 1) % points.count
                let previousIndex = index < 1 ? points.count - 1 : index - 1
    
                let point0 = points[previousIndex]
                let point1 = points[index]
                let point2 = points[nextIndex]
                let point3 = points[nextNextIndex]
    
                let d1 = hypot(CGFloat(point1.x - point0.x), CGFloat(point1.y - point0.y))
                let d2 = hypot(CGFloat(point2.x - point1.x), CGFloat(point2.y - point1.y))
                let d3 = hypot(CGFloat(point3.x - point2.x), CGFloat(point3.y - point2.y))
    
                let d1a2 = pow(d1, alpha * 2)
                let d1a  = pow(d1, alpha)
                let d2a2 = pow(d2, alpha * 2)
                let d2a  = pow(d2, alpha)
                let d3a2 = pow(d3, alpha * 2)
                let d3a  = pow(d3, alpha)
    
                var controlPoint1: CGPoint, controlPoint2: CGPoint
    
                if abs(d1) < kEPSILON {
                    controlPoint1 = point2
                } else {
                    controlPoint1 = (point2 * d1a2 - point0 * d2a2 + point1 * (2 * d1a2 + 3 * d1a * d2a + d2a2)) / (3 * d1a * (d1a + d2a))
                }
    
                if abs(d3) < kEPSILON {
                    controlPoint2 = point2
                } else {
                    controlPoint2 = (point1 * d3a2 - point3 * d2a2 + point2 * (2 * d3a2 + 3 * d3a * d2a + d2a2)) / (3 * d3a * (d3a + d2a))
                }
    
                addCurve(to: point2, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
            }
    
            if closed { close() }
        }
    
    }
    
    // Some functions to make the Catmull-Rom splice code a little more readable.
    // These multiply/divide a `CGPoint` by a scalar and add/subtract one `CGPoint`
    // from another.
    
    func * (lhs: CGPoint, rhs: CGFloat) -> CGPoint {
        return CGPoint(x: lhs.x * rhs, y: lhs.y * CGFloat(rhs))
    }
    
    func / (lhs: CGPoint, rhs: CGFloat) -> CGPoint {
        return CGPoint(x: lhs.x / rhs, y: lhs.y / CGFloat(rhs))
    }
    
    func + (lhs: CGPoint, rhs: CGPoint) -> CGPoint {
        return CGPoint(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
    }
    
    func - (lhs: CGPoint, rhs: CGPoint) -> CGPoint {
        return CGPoint(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
    }
    

    以下是“简单”平滑算法、“Hermite”样条曲线和“Catmull Rom”样条曲线,分别为红色、蓝色和绿色。正如您所看到的,“简单”平滑算法在计算上更简单,但通常不会经过许多点(但提供了更显着的平滑,消除了笔划中的任何不稳定)。像这样跳跃的点夸大了行为,而在标准的“手势”中,它提供了相当不错的平滑效果。另一方面,样条曲线在穿过数组中的点时平滑曲线。

    enter image description here

  2. 如果针对 iOS 9 及更高版本,它会引入一些不错的功能,特别是:

    • 合并触摸,以防用户使用具有此类功能的设备,尤其是较新的 iPad。最重要的是,这些设备(但不是它们的模拟器)每秒能够生成超过 60 次触摸,因此您可以为每次调用报告多次触摸touchesMoved.

    • 预测触摸,设备可以向您显示它预计用户触摸将进行的位置(从而减少绘图中的延迟)。

    将这些放在一起,您可能会执行以下操作:

    var points: [CGPoint]?
    var path: UIBezierPath?
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            points = [touch.location(in: view)]
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            if #available(iOS 9.0, *) {
                if let coalescedTouches = event?.coalescedTouches(for: touch) {
                    points? += coalescedTouches.map { $0.location(in: view) }
                } else {
                    points?.append(touch.location(in: view))
                }
    
                if let predictedTouches = event?.predictedTouches(for: touch) {
                    let predictedPoints = predictedTouches.map { $0.location(in: view) }
                    pathLayer.path = UIBezierPath(catmullRomInterpolatedPoints: points! + predictedPoints, closed: false, alpha: 0.5)?.cgPath
                } else {
                    pathLayer.path = UIBezierPath(catmullRomInterpolatedPoints: points!, closed: false, alpha: 0.5)?.cgPath
                }
            } else {
                points?.append(touch.location(in: view))
                pathLayer.path = UIBezierPath(catmullRomInterpolatedPoints: points!, closed: false, alpha: 0.5)?.cgPath
            }
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        path = UIBezierPath(catmullRomInterpolatedPoints: points!, closed: false, alpha: 0.5)
        pathLayer.path = path?.cgPath
    }
    

    在此代码片段中,我通过更新来渲染路径CAShapeLayer,但如果您想以其他方式呈现它,请随意。例如,使用您的drawRect方法,你会更新path,然后调用setNeedsDisplay().

    并且,上面说明了if #available(iOS 9, *) { ... } else { ... }如果您需要支持 9.0 之前的 iOS 版本,则可以使用此语法,但显然,如果您仅支持 iOS 9 及更高版本,则可以删除该检查并丢失else clause.

    欲了解更多信息,请观看 WWDC 2015 视频iOS 上的高级触摸输入.

无论如何,这会产生类似的结果:

enter image description here

(对于上述内容的 Swift 2.3 版本,请参阅以前的版本这个答案。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绘图类绘制直线而不是曲线 的相关文章

  • 如何读取本地 JSON 文件进行测试

    我正在尝试编写用于 json 验证的单元测试 因为该应用程序严重依赖于来自 REST API 的 json 我有一个包含简单 json 的本地文件 goodFeaturedJson txt 内容 test TEST 测试用例 void te
  • Java JPanel 绘制形状

    我第一次使用 JPanel 并在 JPanel 上绘制基本形状 我已经为这样的形状编写了代码 public class Shape extends JPanel int x y public Shape int x int y this x
  • 将数据追加到 UITableView 的正确方法,swift

    我正在尝试使用两种方式将新数据添加到 UITableView 第一种方式 func insertData appendMessages Message var currentCount self messeges count var ind
  • iOS 从另一个类更新 ViewController UILabel

    我是开发新手 一直在用头撞墙试图弄清楚这一点 我确信 我错过了一些愚蠢的东西 但在尝试了各种不同的解决方案后 我仍然无法得到结果我在寻找 我希望能够从另一个类更新 ViewController 中的 UILabel 这是一个我无法运行的小演
  • 在 Mac OS 10.14.2 上的 Python 3.7 中安装 JPype1 时出错

    我在系统中安装 JPype1 时遇到错误 我正在使用Python 3 7 JPype1 是 Jaydebeapi 的依赖项 pip install Jpype1 以下是错误消息 Collecting jpype1 Using cached
  • 以编程方式设置 UITextField 占位符颜色

    如何在 swift 中以编程方式设置 UITextField 占位符颜色 1 创建一个具有所需颜色的 AttributedString 2 将此 AttributedString 设置为文本字段 attributePlaceholder 属
  • dyld:未找到符号:___NSDictionary0__ 在 XCode 7 和 iOS 目标 9.0 中使用带有发现文档的 google ServiceGenerator 二进制文件时

    我正在尝试使用 Google 从 Google 后端生成客户端 API 代码serviceGenerator以发现文档作为输入 以下是确切的命令 Users raja Library Developer Xcode DerivedData
  • CC Parallax 节点视差比(说明)

    我觉得问这个问题很愚蠢 但我在任何地方都找不到明确的答案 或者根本找不到答案 所以我觉得我必须问 有没有人可以清楚地解释 CCParallaxNode 的 parallaxRatio 是如何工作的 我检查了CCParallaxNode的来源
  • 如何获取 iTunes connect 团队 ID 和团队名称?

    我正在写下一个Appfile for fastlane 我的问题是我已经有了team name and team id在 Apple 开发中心 但我无法获取iTunes Connect ID itc team id 我正在与不同的团队合作
  • 使 MKMapView 只放大 centerCoordinate 吗?

    我有一个始终位于地图中心的 MKPinAnnotationView 平移和缩放时 该图钉为我提供了地图的中心坐标 纬度 经度 目前 当您放大时 它只会放大您指定地图放大的位置 我真的很想将变焦锁定在图钉上 关于我如何实现这一目标有什么想法吗
  • iOS 8 上的 NSRangeException

    我将 XCode 5 升级到 6 以便在 iPhone 6 和 6 Plus 上测试我的项目 但在启动时遇到了神秘的崩溃 NSArrayM objectAtIndex index 4 beyond bounds 0 3 First thro
  • iOS 上服务器发送事件的 webkit 的推荐替代方案 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我想在我的本机 iOS 应用程序中接收服务器发送的事件 但我不使用 webkit Safari 根据我
  • 动态获取协议的字符串表示形式

    我正在寻找一种从协议类型动态获取协议名称的方法 而不必使用 objc协议声明中的属性 我知道这有效 func keyForProtocol aProtocol Protocol gt String return NSStringFromPr
  • iOS - 当应用程序被终止时处理静默推送通知

    我目前在 iOS 中遇到推送通知问题 我的应用程序收到包含标识符的静默推送通知 然后 该标识符用于从创建本地通知的远程服务器获取数据 据我所知 如果用户强制退出应用程序 即通过双击主页按钮并滑动关闭应用程序 则静默推送通知不会传递到 App
  • 扩展功能截图未捕获 iPhone 和 iPad 中的同一区域

    我正在使用扩展函数来截取 uiview 的屏幕截图 问题是结果看起来非常不同 我希望无论使用 ipad 还是 iphone 照片看起来都一样 我手动输入约束 因此我希望图像是相同的 我想使用该函数转到视图控制器的原点或中心 高度为 200
  • 针对架构 armv7 的 iOS 链接器错误的 Google 转化跟踪

    我正在尝试将 iOS 版 Google 转化跟踪功能添加到我的 iPhone 应用程序中 该应用程序的基础 SDK 是 iOS6 该应用程序的有效架构是armv7 armv7s 该应用程序的 iOS 部署目标是 4 3 我正在使用最新的 x
  • 在UIView中画线

    我需要在 UIView 中画一条水平线 最简单的方法是什么 例如 我想在 y coord 200 处画一条黑色水平线 我没有使用界面生成器 也许这有点晚了 但我想补充一点 有更好的方法 使用 UIView 很简单 但相对较慢 此方法会覆盖视
  • 如何从 Xcode 中导航栏上的后退按钮中删除文本?

    我通过在应用程序委托中添加以下代码来将箭头自定义图像设置为导航栏 它可以工作 但现在我希望完全删除后退按钮的文本 UIImage backButtonImage UIImage imageNamed BackButtonGrey png b
  • 为 CocoaPods 插件设置 Xcode

    我正在尝试设置终端 Xcode 以便正确工作Xcode CocoaPods 插件 https github com kattrali cocoapods xcode plugin 当我从插件运行集成 cocoapods 选项时 我收到一条消
  • Xcode 8.3 / Xcode 9.0 刷新配置文件设备

    我添加了一些新设备 当 Xcode 8 自动管理签名资产时 如何刷新配置文件 我发现了这个问题 刷新 Xcode 7 管理的团队配置文件中的设备 https stackoverflow com questions 32729193 refr

随机推荐

  • 为什么 printf 将 8 位字符填充为 32 位?

    char byte 0xff printf lu n sizeof byte Output is 1 printf x n byte Output is ffffffff 如果尺寸为byte只有一个字节 那为什么printf 表现得好像它是
  • 计算Python数组中每5个元素的总和

    我有一个 python 数组 我想计算其中每 5 个元素的总和 就我而言 我有数组c有十个元素 实际上它还有更多的元素 c 1 0 0 0 0 2 0 0 0 0 所以最后我想要一个新的数组 c new 应显示前 5 个元素和后 5 个元素
  • 在 bash 中重定向 C 程序输出时出现问题

    我用 C 编写了一个程序 使用 printf 将消息发送到标准输出 但在将输出重定向到文件 从 bash 运行 时遇到问题 我试过了 program argument gt gt program out program argument g
  • 磁盘已满时删除文件的 Shell 脚本

    如果缓存目录变得太大 我正在编写一个小脚本 每天通过 CRON 清理 Linux 上的空间 由于我对 bash 脚本编写非常陌生 因此我需要 Linux 专家的一些帮助 这是基本上的逻辑 伪代码 if Drive Space Left lt
  • 如何在 C# 中更改 Windows 窗体上 Groupbox 的边框粗细?

    我没有找到任何解决方案可以帮助我解决有关 SO 的旧问题 是否可以通过更改颜色使它们更厚或更明显 如果是的话 一些代码会很棒 或者只是提示如何做到这一点 您需要进行定制GroupBox控制 看The Grouper 自定义 Groupbox
  • WPF 按钮相同/推荐宽度

    假设您有一个带有多个按钮的窗口 例如确定 取消或是 否 取消 所有按钮的宽度必须相同 显然 这可以通过猜测一个数字并将它们全部硬连接到该数字来完成 有没有更好的方法来做到这一点 一种会考虑首选 推荐尺寸的方法 确定 按钮应该有多宽 这不是一
  • 在Android模拟器中使用相机

    我希望使用网络摄像头在 Android 模拟器中模拟摄像头 基本上我只需要用模拟器中的相机拍照 不需要实时预览 即如果它使它更容易 我按照教程进行操作here这是我能找到的唯一一个接近我的要求的 但是该教程中使用的许多库 如 android
  • 如何计算猫鼬中具有一个不同字段的记录?

    在探索 Nodejs 的 mongoose 时 我遇到了需要知道我的集合中用户数量的问题 我的收藏有记录 每条记录都有一个用户 我想知道独特 不同 用户的数量 我怎样才能用猫鼬做到这一点 EDIT 数据库增长得非常快 是否有办法从数据库中获
  • Django ImageField 将可调用对象传递给 upload_to

    我正在尝试将自定义 upload to 函数传递给我的模型 imageField 但我想将该函数定义为模型函数 这可能吗 class MyModel models Model image models ImageField upload t
  • gganimate 绘图,其中点保留而线淡出

    这是一个可重现的静态图示例 我想要为其制作动画 我想展示 MCMC 采样器的行为方式 library tidyverse library gganimate set seed 1234 plot data lt tibble x cumsu
  • 更改 PDF 文件的文本和背景颜色

    我想以编程方式更改 PDF 文档中的背景颜色和文本颜色 以便它们在晚上更适合阅读 有点像在 Adob e Reader 中 编辑 gt 首选项 gt 辅助功能 gt 替换文档颜色 有没有好的 Windows 命令行工具或 API 可以做到这
  • 如何以编程方式绑定到静态属性?

    如何以编程方式绑定到静态属性 我可以用 C 来制作什么 Binding Source x Static local MyClass StaticProperty Update 是否可以进行 OneWayToSource 绑定 我知道 Two
  • d3 v4 geo绘制倒置边界

    当我在 SVG 元素中绘制百慕大三角形时 比例不是我所期望的 三角形应该延伸到框的边缘 并且填充是向后的 不是绘制三角形 而是绘制一个切掉三角形的正方形 var geojson features type Feature propertie
  • 在选定选项更改时显示和隐藏 html 元素

    在 JSP 页面中 我有一个下拉列表 当选择列表的第一个元素时 我希望在单击时显示一个文本区域 我是 Javascript Jquery 的新手 所以我显然在函数中遗漏了一些东西 文本区域从未显示 希望有人能帮忙 这是 HTML tr cl
  • 模型上的猫鼬 findOne 是否返回承诺?

    我有一个简单的 Node 模块 它导出一个进行数据库查询的函数 问题是该函数在数据库查询完成之前返回 use strict var mongoose require mongoose Model require entities user
  • 安装的python3.9在linux中不显示

    我按照此中的步骤安装了 python 3 9link sudo apt update sudo apt install python3 9 python3 9 sudo update alternatives install usr bin
  • 如何确定嵌入式系统中的最大堆栈使用率?

    当我给Keil编译器 callgraph 选项时 它为我静态计算准确的 最大堆栈使用量 唉 今天它给了我一条 最大堆栈使用量 284 字节 未知 没有堆栈大小的函数 消息 以及 没有堆栈信息的函数 列表 Nigel Jones 表示递归在嵌
  • 我是否需要在生产服务器上安装 Node.js 来托管 Angular 2?

    我正在尝试在我们的实时服务器 Windows 2008R2 IIS 7 5 服务器 上部署我的第一个 Angular 2 应用程序 MVC部分运行良好 我可以看到登录用户名并显示 MVC 布局的框架 但它总是将我重定向到 Error csh
  • 在“_strong id”类型的对象上找不到属性“标签”

    我正在根据本教程构建一个应用程序 http bit ly NI9kQe 它使用自定义 Web api 连接到 Web 服务器 要求之一是检测是否已点击 登录 或 注册 按钮 这是使用在界面生成器中为按钮设置的 标签 来完成的 注册按钮的标签
  • 绘图类绘制直线而不是曲线

    我有下面的代码 使用 UIBezierPath 绘制线条 代码使用addCurveToPoint它应该使用三次贝塞尔路径绘制曲线 但是代码的最终结果是绘制连接的直线 但是addLineToPoint没有被使用 可能发生了什么 为什么代码不绘