UIImage 裁剪出透明像素
UIImage Crop Out Transparent Pixels
使用 swift 4.
从 UIImage 裁剪透明像素后,我需要帮助来创建完美清晰的图像
I want to create image with remove transparent color, So for that i
have used below code. But it blur image with small small square
rectangles appears.
使用以下代码
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;
}
}
在这一行中检查 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 > 0.5)
甚至更高。 if (a == 1)
将是限制,它会保证完全没有透明度。
如果您希望 imageCroppedBg
的大小与 imgWithClearBackgroung
相同,则将包含 imgWithClearBackgroung
的 UIImageView
的内容模式设置为 .scaleAspectFit
:
let imageCroppedBg = imgWithClearBackgroung.cropAlpha()
imageView.image = imageCroppedBg
imageView.contentMode = .scaleAspectFit
有关内容模式的更多信息,请查看 here。
P.S: 在 Swift 中,行尾不需要 ;
。
使用 swift 4.
从 UIImage 裁剪透明像素后,我需要帮助来创建完美清晰的图像I want to create image with remove transparent color, So for that i have used below code. But it blur image with small small square rectangles appears.
使用以下代码
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;
}
}
在这一行中检查 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 > 0.5)
甚至更高。 if (a == 1)
将是限制,它会保证完全没有透明度。
如果您希望 imageCroppedBg
的大小与 imgWithClearBackgroung
相同,则将包含 imgWithClearBackgroung
的 UIImageView
的内容模式设置为 .scaleAspectFit
:
let imageCroppedBg = imgWithClearBackgroung.cropAlpha()
imageView.image = imageCroppedBg
imageView.contentMode = .scaleAspectFit
有关内容模式的更多信息,请查看 here。
P.S: 在 Swift 中,行尾不需要 ;
。