【问题标题】:How to set outline as per image shape?如何根据图像形状设置轮廓?
【发布时间】:2022-01-06 08:40:24
【问题描述】:

我坚持这一点。我想要一个按照图像的轮廓,我想要按照这个video输出

我试过这段代码,但运行不顺畅。

extension CGPoint {
        /**
        Rotates the point from the center `origin` by `byDegrees` degrees along the Z axis.
    
        - Parameters:
            - origin: The center of he rotation;
            - byDegrees: Amount of degrees to rotate around the Z axis.
    
        - Returns: The rotated point.
        */
        func rotated(around origin: CGPoint, byDegrees: CGFloat) -> CGPoint {
            let dx = x - origin.x
            let dy = y - origin.y
            let radius = sqrt(dx * dx + dy * dy)
            let azimuth = atan2(dy, dx) // in radians
            let newAzimuth = azimuth + byDegrees * .pi / 180.0 // to radians
            let x = origin.x + radius * cos(newAzimuth)
            let y = origin.y + radius * sin(newAzimuth)
            return CGPoint(x: x, y: y)
        }
    }
    
    public extension UIImage {
    
        /**
        Returns the flat colorized version of the image, or self when something was wrong
    
        - Parameters:
            - color: The colors to user. By defaut, uses the ``UIColor.white`
    
        - Returns: the flat colorized version of the image, or the self if something was wrong
        */
        func colorized(with color: UIColor = .white) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(size, false, scale)
    
            defer {
                UIGraphicsEndImageContext()
            }
    
            guard let context = UIGraphicsGetCurrentContext(), let cgImage = cgImage else { return self }
    
    
            let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    
            color.setFill()
            context.translateBy(x: 0, y: size.height)
            context.scaleBy(x: 1.0, y: -1.0)
            context.clip(to: rect, mask: cgImage)
            context.fill(rect)
    
            guard let colored = UIGraphicsGetImageFromCurrentImageContext() else { return self }
    
            return colored
        }
    
        /**
        Returns the stroked version of the fransparent image with the given stroke color and the thickness.
    
        - Parameters:
            - color: The colors to user. By defaut, uses the ``UIColor.white`
            - thickness: the thickness of the border. Default to `2`
            - quality: The number of degrees (out of 360): the smaller the best, but the slower. Defaults to `10`.
    
        - Returns: the stroked version of the image, or self if something was wrong
        */
    
        func stroked(with color: UIColor = .white, thickness: CGFloat = 2, quality: CGFloat = 10) -> UIImage {
    
            guard let cgImage = cgImage else { return self }
    
            // Colorize the stroke image to reflect border color
            let strokeImage = colorized(with: color)
    
            guard let strokeCGImage = strokeImage.cgImage else { return self }
    
            /// Rendering quality of the stroke
            let step = quality == 0 ? 10 : abs(quality)
    
            let oldRect = CGRect(x: thickness, y: thickness, width: size.width, height: size.height).integral
            let newSize = CGSize(width: size.width + 2 * thickness, height: size.height + 2 * thickness)
            let translationVector = CGPoint(x: thickness, y: 0)
    
            UIGraphicsBeginImageContextWithOptions(newSize, false, scale)
    
            guard let context = UIGraphicsGetCurrentContext() else { return self }
    
            defer {
                UIGraphicsEndImageContext()
            }
            context.translateBy(x: 0, y: newSize.height)
            context.scaleBy(x: 1.0, y: -1.0)
            context.interpolationQuality = .high
    
            for angle: CGFloat in stride(from: 0, to: 360, by: step) {
                let vector = translationVector.rotated(around: .zero, byDegrees: angle)
                let transform = CGAffineTransform(translationX: vector.x, y: vector.y)
    
                context.concatenate(transform)
    
                context.draw(strokeCGImage, in: oldRect)
    
                let resetTransform = CGAffineTransform(translationX: -vector.x, y: -vector.y)
                context.concatenate(resetTransform)
            }
    
            context.draw(cgImage, in: oldRect)
    
            guard let stroked = UIGraphicsGetImageFromCurrentImageContext() else { return self }
    
            return stroked
        }
    }

【问题讨论】:

标签: ios swift iphone image draw


【解决方案1】:

试试这个 除了 Storyboard 中的 top/left 之外,PLS 不会向 imageView 添加约束。

//
//  ViewController.swift
//  AddBorderANDZoom
//
//  Created by ing.conti on 06/01/22.
//

import UIKit

import CoreGraphics
import AVFoundation


class ViewController: UIViewController {
    
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var slider: UISlider!
    let maZoom = 20.0
    
    override func viewDidLoad() {
        super.viewDidLoad()

       self.slider.value = 1
        self.imageView.image = UIImage(named: "apple")?.outline(borderSize: self.maZoom)
        
    }
    @IBAction func didSlide(_ sender: UISlider) {
   
        let value = CGFloat(sender.value  * 20)
        self.imageView.image = UIImage(named: "apple")?.outline(borderSize: value)
        
    }
    
}


/////


extension UIImage {
    func outline(borderSize: CGFloat = 6.0) -> UIImage? {
        
        let color = UIColor.black
        let W = self.size.width
        let H = self.size.height
        
        let scale = 1 + (borderSize/W)
                
        let outlinedImageRect = CGRect(x: -borderSize/2,
                                       y: -borderSize/2,
                                       width: W * scale,
                                       height: H * scale)
        let imageRect = CGRect(x: 0,
                               y: 0,
                               width: W,
                               height: H)
        
    
        UIGraphicsBeginImageContextWithOptions(outlinedImageRect.size, false, scale)
        
        self.draw(in: outlinedImageRect)
        
        let context = UIGraphicsGetCurrentContext()!
        context.setBlendMode(.sourceIn)
        
        context.setFillColor(color.cgColor)
        context.fill(outlinedImageRect)
        self.draw(in: imageRect)
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return newImage   
    }        
}

【讨论】:

    猜你喜欢
    • 2015-08-09
    • 1970-01-01
    • 2021-09-06
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多