【问题标题】:Draw border around content of UIImageView在 UIImageView 的内容周围绘制边框
【发布时间】:2014-11-06 14:00:28
【问题描述】:

我有一个带有图像的 UIImageView,它是一辆具有透明背景的汽车:

我想在汽车周围画一个边框:

我怎样才能达到这个效果?

目前,我已经以这种方式测试过CoreGraphics,但没有很好的结果:

    // load the image
    UIImage *img = carImage;

    UIGraphicsBeginImageContext(img.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor redColor] setFill];
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, img.size.width * 1.1, img.size.height*1.1);
    CGContextDrawImage(context, rect, img.CGImage);

    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

有什么帮助吗?谢谢。

【问题讨论】:

    标签: ios xcode image uiimageview draw


    【解决方案1】:

    这就是我所做的:

    我是在 Swift 中完成的,只是为了在 playgrounds 中检查它,认为您可以轻松地将其转换为 Objective-C

    import UIKit
    
    
    func drawOutlie(#image:UIImage, color:UIColor) -> UIImage
    {
      var newImageKoef:CGFloat = 1.08
      
      var outlinedImageRect = CGRect(x: 0.0, y: 0.0, width: image.size.width * newImageKoef, height: image.size.height * newImageKoef)
      
      var imageRect = CGRect(x: image.size.width * (newImageKoef - 1) * 0.5, y: image.size.height * (newImageKoef - 1) * 0.5, width: image.size.width, height: image.size.height)
      
      UIGraphicsBeginImageContextWithOptions(outlinedImageRect.size, false, newImageKoef)
      
      image.drawInRect(outlinedImageRect)
      
      var context = UIGraphicsGetCurrentContext()
      CGContextSetBlendMode(context, kCGBlendModeSourceIn)
      
      CGContextSetFillColorWithColor(context, color.CGColor)
      CGContextFillRect(context, outlinedImageRect)
      image.drawInRect(imageRect)
      
      var newImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      
      return newImage
      
    }
    
    var imageIn = UIImage(named: "158jM")
    
    var imageOut = drawOutlie(image: imageIn, UIColor.redColor())
    

    那么它是如何工作的呢?

    1. 我们创建干净的上下文(也称为画布),其尺寸比原始图像(用于轮廓)稍大
    2. 我们在整个画布上绘制图像
    3. 我们用颜色填充该图像
    4. 我们在顶部绘制较小的图像

    您可以通过更改此属性来更改轮廓大小:var newImageKoef:CGFloat = 1.08

    这是我在操场上得到的结果

    斯威夫特 5:

    extension UIImage {
        
        func drawOutlie(imageKeof: CGFloat = 0.2, color: UIColor = .white)-> UIImage? {
            let outlinedImageRect = CGRect(x: 0.0, y: 0.0, width: size.width * imageKeof, height: size.height * imageKeof)
            let imageRect = CGRect(x: self.size.width * (imageKeof - 1) * 0.5, y: self.size.height * (imageKeof - 1) * 0.5, width: size.width, height: size.height)
            UIGraphicsBeginImageContextWithOptions(outlinedImageRect.size, false, imageKeof)
            draw(in: outlinedImageRect)
            let context = UIGraphicsGetCurrentContext()
            context!.setBlendMode(.sourceIn)
            context!.setFillColor(color.cgColor)
            context!.fill(outlinedImageRect)
            draw(in: imageRect)
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage
        }
    }
    

    【讨论】:

    • 我无法使用这行代码获得完全相同的结果。我已转换为 Swift 5。边框未与图像对齐。
    • 不好的解决方案,因为它是通过缩放绘制相同的图像,导致笔画不均匀
    • 这个解决方案很完美,stackoverflow.com/a/61413701/1599713
    • 这并没有在图像的所有部分周围创建相等的边框。
    【解决方案2】:

    Swift 5

    的haawa答案
    extension UIImage {
        func drawOutlie(imageKeof: CGFloat = 1.01, color: UIColor) -> UIImage? {
    
            let outlinedImageRect = CGRect(x: 0.0, y: 0.0,
                                       width: size.width * imageKeof,
                                       height: size.height * imageKeof)
    
            let imageRect = CGRect(x: size.width * (imageKeof - 1) * 0.5,
                               y: size.height * (imageKeof - 1) * 0.5,
                               width: size.width,
                               height: size.height)
    
            UIGraphicsBeginImageContextWithOptions(outlinedImageRect.size, false, imageKeof)
    
            draw(in: outlinedImageRect)
    
            guard let context = UIGraphicsGetCurrentContext() else {return nil}
            context.setBlendMode(.sourceIn)
            context.setFillColor(color.cgColor)
            context.fill(outlinedImageRect)
            draw(in: imageRect)
    
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return newImage
        }
    }
    

    【讨论】:

      【解决方案3】:

      这种方法有点不同,但更简单。

      imageView.layer.shadowColor = UIColor.red.cgColor
      imageView.layer.shadowOffset = CGSize(width: 0, height: 0)
      imageView.layer.shadowOpacity = 1
      imageView.layer.shadowRadius = 10.0
      imageView.clipsToBounds = false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-10
        • 2020-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多