Ios 如何在不进行抗锯齿的情况下调整UIImage的大小?

Ios 如何在不进行抗锯齿的情况下调整UIImage的大小?,ios,swift,drawing,image-resizing,Ios,Swift,Drawing,Image Resizing,我正在开发一款iOS棋盘游戏。我试图给这块板一种“质感” 我所做的是创建了这个非常小的图像(非常小,请务必仔细查看): 我将此图像传递给UIColor.init(patternImage:)初始值设定项,以创建一个UIColor即此图像。我用这个UIColor填充了一些正方形UIBezierPaths,结果如下所示: // this is the code I used to draw a single square let path = UIBezierPath(rect: CGRect(o

我正在开发一款iOS棋盘游戏。我试图给这块板一种“质感”

我所做的是创建了这个非常小的图像(非常小,请务必仔细查看):

我将此图像传递给
UIColor.init(patternImage:)
初始值设定项,以创建一个
UIColor
即此图像。我用这个
UIColor
填充了一些正方形
UIBezierPath
s,结果如下所示:

// this is the code I used to draw a single square
let path = UIBezierPath(rect: CGRect(origin: point(for: Position(x, y)), size: CGSize(width: squareLength, height: squareLength)))
UIColor.black.setStroke()
path.lineWidth = strokeWidth
// this is the line that's important!
UIColor(patternImage: #imageLiteral(resourceName: 

该图像的所有副本完美地排列在一起,并形成许多对角线。到目前为止还不错

现在在iPad上,我画的方块会更大,这些方块的边界也会更大。我已经成功地计算了正方形的笔划宽度和大小,所以这不是问题

然而,由于iPad上的正方形更大,因此每个正方形会有更多的对角线。我不想那样。我需要将非常小的图像调整为较大的图像,大小取决于正方形的笔划宽度。具体来说,调整大小的图像的宽度应为笔划宽度的两倍

我编写此扩展以调整图像大小,根据以下内容改编:

并这样称呼它:

// this is the code I used to draw a single square
let path = UIBezierPath(rect: CGRect(origin: point(for: Position(x, y)), size: CGSize(width: squareLength, height: squareLength)))
UIColor.black.setStroke()
path.lineWidth = strokeWidth
// this is the line that's important!
UIColor(patternImage: #imageLiteral(resourceName: 
“纹理”)。已调整大小(toWidth:strokeWidth*2))。设置填充() path.fill() path.stroke()

现在,iPhone上的游戏板看起来像这样:

你可能需要把网页放大一点,看看我的意思。董事会现在看起来非常难看。您可以看到图像每个副本的“边框”。我不要这个。不过在iPad上,这块板看起来不错。我怀疑只有在缩小图像时才会发生这种情况

我想这可能是由于使用扩展时发生的抗锯齿。我找到并开始删除抗锯齿,但前者似乎是在图像视图中执行此操作,而我在自定义
GameBoardView的
draw(:)
方法中执行此操作。后者的解决方案似乎与我使用的完全相同

如何在没有抗锯齿的情况下调整大小?或者在更高的抽象层次上,如何使我的板看起来漂亮?

类规则:UIView{
class Ruled: UIView {

    override func draw(_ rect: CGRect) {

        let T: CGFloat = 15     // desired thickness of lines
        let G: CGFloat = 30     // desired gap between lines
        let W = rect.size.width
        let H = rect.size.height

        guard let c = UIGraphicsGetCurrentContext() else { return }
        c.setStrokeColor(UIColor.orange.cgColor)
        c.setLineWidth(T)

        var p = -(W > H ? W : H) - T
        while p <= W {

            c.move( to: CGPoint(x: p-T, y: -T) )
            c.addLine( to: CGPoint(x: p+T+H, y: T+H) )
            c.strokePath()
            p += G + T + T
        }
    }
}
重写函数绘图(rect:CGRect){ 设T:CGFloat=15//所需的线条厚度 设G:CGFloat=30//线之间的期望间隙 设W=rect.size.width 设H=rect.size.height guard let c=UIGraphicsGetCurrentContext()else{return} c、 设置行程颜色(UIColor.orange.cgColor) c、 设置线宽(T) var p=-(W>H?W:H)-T 而p高度?宽度:高度 c、 saveGState() c、 夹(至:r) var p=x-朗格赛德
而p这不适用于我的具体情况。所有的方块本身都不是
UIView
s。我在一个大
UIView
中绘制了带有路径的方块,作为“父”。正方形不能填充大视图,并且有一些周围的空间。您的代码将在每个正方形的边界之外绘制。这会导致正方形周围的空白也被对角线覆盖。您可以将其修改为仅在每个正方形的边界内绘制对角线吗?嘿,您只需在小视图上设置剪裁@清扫者。但我有另一个版本,我将包括。然而,你需要投票“向上”任何与此答案或其他答案工作量相同的答案。编辑中的代码有效。谢谢。除非其他人能够在不使用抗锯齿的情况下调整图像大小,否则我会给你奖金,因为这是最初的问题。呵呵,好的一个@Sweeper,希望它能有所帮助。(不幸的是,没有办法实现这样的抗锯齿。)顺便说一句,你会非常喜欢:这是做这类事情的更现代的方式。干杯!@Sweeper可能是你将targetSize设置为你需要的图像大小
func simpleStripes(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
     
    let stripeWidth: CGFloat = 20.0 // whatever you want
    let m = stripeWidth / 2.0
    
    guard let c = UIGraphicsGetCurrentContext() else { return }
    c.setLineWidth(stripeWidth)
    
    let r = CGRect(x: x, y: y, width: width, height: height)
    let longerSide = width > height ? width : height
    
    c.saveGState()
    c.clip(to: r)
        
        var p = x - longerSide
        while p <= x + width {
            
            c.setStrokeColor(pale blue)
            c.move( to: CGPoint(x: p-m, y: y-m) )
            c.addLine( to: CGPoint(x: p+m+height, y: y+m+height) )
            c.strokePath()
            
            p += stripeWidth
            
            c.setStrokeColor(pale gray)
            c.move( to: CGPoint(x: p-m, y: y-m) )
            c.addLine( to: CGPoint(x: p+m+height, y: y+m+height) )
            c.strokePath()
            
            p += stripeWidth
        }
        
    c.restoreGState()
}
extension UIImage {

    func ResizeImage(targetSize: CGSize) -> UIImage
    {
        let size = self.size

        let widthRatio  = targetSize.width  / self.size.width
        let heightRatio = targetSize.height / self.size.height

        // Figure out what our orientation is, and use that to form the rectangle
        var newSize: CGSize
        if(widthRatio > heightRatio) {
            newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
        } else {
            newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
        }
        // This is the rect that we've calculated out and this is what is actually used below
        let rect = CGRect(x: 0, y: 0, width: newSize.width,height: newSize.height)

        // Actually do the resizing to the rect using the ImageContext stuff
        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        self.draw(in: rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }
}