【问题标题】:UIImage Crop Out Transparent PixelsUIImage 裁剪出透明像素
【发布时间】:2019-01-26 09:41:18
【问题描述】:

在使用 swift 4 从 UIImage 裁剪透明像素后,我需要帮助创建完美清晰的图像。

我想创建删除透明颜色的图像,所以我 已使用以下代码。但它会用小方块模糊图像 出现矩形。

使用下面的代码

let imageCroppedBg = imgWithClearBackgroung!.cropAlpha()


//UIImage extension
extension UIImage {
    func cropAlpha() -> UIImage {
        
        let cgImage = self.cgImage!;
        
        let width = cgImage.width
        let height = cgImage.height
        
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bytesPerPixel:Int = 4
        let bytesPerRow = bytesPerPixel * width
        let bitsPerComponent = 8
        let bitmapInfo: UInt32 = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue
        
        guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo),
            let ptr = context.data?.assumingMemoryBound(to: UInt8.self) else {
                return self
        }
        
        context.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: width, height: height))
        
        var minX = width
        var minY = height
        var maxX: Int = 0
        var maxY: Int = 0
        
        for x in 1 ..< width {
            for y in 1 ..< height {
                
                let i = bytesPerRow * Int(y) + bytesPerPixel * Int(x)
                let a = CGFloat(ptr[i + 3]) / 255.0
                
                if(a>0) {
                    if (x < minX) { minX = x };
                    if (x > maxX) { maxX = x };
                    if (y < minY) { minY = y};
                    if (y > maxY) { maxY = y};
                }
            }
        }
        
        let rect = CGRect(x: CGFloat(minX),y: CGFloat(minY), width: CGFloat(maxX-minX), height: CGFloat(maxY-minY))
        let imageScale:CGFloat = self.scale
        let croppedImage =  self.cgImage!.cropping(to: rect)!
        let ret = UIImage(cgImage: croppedImage, scale: imageScale, orientation: self.imageOrientation)
        
        return ret;
    }
}

【问题讨论】:

  • 我不太明白这个问题,你能发布一个具有透明像素的 UIImage 的示例吗?你从哪里得到那个图像?您是否以编程方式创建它?是的,我确实看到边缘有些模糊,我理解这个问题,但我不知道如何重现这个问题以便准确地说明发生了什么。
  • @MihaiErős,我也加了透明图,供大家参考。
  • 我看到“裁剪”之前的图像在边框上有那些方块。

标签: ios swift uiimage core-graphics crop


【解决方案1】:

在这一行中检查 alpha 分量是否只是 > 零可能还不够:

if(a>0) {
    if (x < minX) { minX = x }
    if (x > maxX) { maxX = x }
    if (y < minY) { minY = y }
    if (y > maxY) { maxY = y }
}

也许您可以提高阈值:if(a &gt; 0.5) 甚至更高。 if (a == 1) 将是限制,它将保证根本没有透明度。

如果您想让imageCroppedBgimgWithClearBackgroung 大小相同,请将包含imgWithClearBackgroungUIImageView 的内容模式设置为.scaleAspectFit

let imageCroppedBg = imgWithClearBackgroung.cropAlpha()
imageView.image = imageCroppedBg
imageView.contentMode = .scaleAspectFit

有关内容模式的更多信息,请查看here

P.S:在 Swift 中,; 不是必需的。

【讨论】:

  • 已注明,已删除 ';'从行尾开始,我也尝试过 0.5 但效果不佳,只是稍作改动。感谢您的回答,您还有其他想法或详细答案吗?提前谢谢你,
  • @Mehul 如果您检查您在 Photoshop 中提供的 png 文件,您将看到额外像素的不透明度为 0.77。所以应该调整门槛。在极端情况下,您可能需要 alpha a == 1
  • 好的,谢谢,我会尽快检查并通知您。 :-)
  • 是的,它有效,但图像拉伸。图像尝试适合原始宽度和高度,因此输出图像看起来像被拉伸。你能帮我得到输出图像的高度和宽度吗,所以我们会在去除 alpha 像素后得到完美的图像。提前致谢。
  • @Mehul 你能用你目前的结果编辑你的问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
  • 2023-03-24
  • 2012-05-15
相关资源
最近更新 更多