Swift3 - 使用 CoreGraphics 或 CoreImage 从图像中裁剪或 trim 无聊像素
Swift3 - Crop-out or trim boring pixels from the image with CoreGraphics or CoreImage
我想知道是否有一种简单的方法可以 trim/crop-out 使用 CoreGraphics 或 CoreImage 和 Swift3 制作图像?
给定顶部的输入图像(具有 red/rose 背景)我们 trim 那些红色像素被移除以便只留下真实、有趣的位?
我刚刚在这里回答了类似问题的一部分:
以下是获取顶部和底部唯一像素的方法。
func pixelNotMatchingTopLeftColor(in image: UIImage, first isFirst: Bool) -> CGPoint? {
let width = Int(image.size.width)
let height = Int(image.size.height)
guard width > 1 || height < 1 else {
return nil
}
if let cfData:CFData = image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfData) {
let cornerPixelRed = pointer.pointee
let cornerPixelGreen = pointer.advanced(by: 0).pointee
let cornerPixelBlue = pointer.advanced(by: 1).pointee
let cornerPixelAlpha = pointer.advanced(by: 2).pointee
let bytesPerpixel = 4
let firstPixel = 1 * bytesPerpixel
let lastPixel = width * height * bytesPerpixel - 1 * bytesPerpixel
let range = isFirst ? stride(from: firstPixel, through: lastPixel, by: bytesPerpixel) :
stride(from: lastPixel, through: firstPixel + bytesPerPixel, by: -bytesPerpixel)
for pixelAddress in range {
if pointer.advanced(by: pixelAddress).pointee != cornerPixelRed || //Red
pointer.advanced(by: pixelAddress + 1).pointee != cornerPixelGreen || //Green
pointer.advanced(by: pixelAddress + 2).pointee != cornerPixelBlue || //Blue
pointer.advanced(by: pixelAddress + 3).pointee != cornerPixelAlpha { //Alpha
print(pixelAddress)
return CGPoint(x: (pixelAddress / bytesPerpixel) % width, y: (pixelAddress / bytesPerpixel) / width)
}
}
}
return nil
}
func firstUniquePixel( in image: UIImage) -> CGPoint? {
return pixelNotMatchingTopLeftColor(in: image, first: true)
}
func lastUniquePixel( in image: UIImage) -> CGPoint? {
return pixelNotMatchingTopLeftColor(in: image, first: false)
}
你必须对左右做类似的事情(如果你在计算指针数学时遇到问题,请告诉我)。然后使用顶部、左侧、底部和右侧调用 CGImageCreateWithImageInRect.
我想知道是否有一种简单的方法可以 trim/crop-out 使用 CoreGraphics 或 CoreImage 和 Swift3 制作图像?
给定顶部的输入图像(具有 red/rose 背景)我们 trim 那些红色像素被移除以便只留下真实、有趣的位?
我刚刚在这里回答了类似问题的一部分:
以下是获取顶部和底部唯一像素的方法。
func pixelNotMatchingTopLeftColor(in image: UIImage, first isFirst: Bool) -> CGPoint? {
let width = Int(image.size.width)
let height = Int(image.size.height)
guard width > 1 || height < 1 else {
return nil
}
if let cfData:CFData = image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfData) {
let cornerPixelRed = pointer.pointee
let cornerPixelGreen = pointer.advanced(by: 0).pointee
let cornerPixelBlue = pointer.advanced(by: 1).pointee
let cornerPixelAlpha = pointer.advanced(by: 2).pointee
let bytesPerpixel = 4
let firstPixel = 1 * bytesPerpixel
let lastPixel = width * height * bytesPerpixel - 1 * bytesPerpixel
let range = isFirst ? stride(from: firstPixel, through: lastPixel, by: bytesPerpixel) :
stride(from: lastPixel, through: firstPixel + bytesPerPixel, by: -bytesPerpixel)
for pixelAddress in range {
if pointer.advanced(by: pixelAddress).pointee != cornerPixelRed || //Red
pointer.advanced(by: pixelAddress + 1).pointee != cornerPixelGreen || //Green
pointer.advanced(by: pixelAddress + 2).pointee != cornerPixelBlue || //Blue
pointer.advanced(by: pixelAddress + 3).pointee != cornerPixelAlpha { //Alpha
print(pixelAddress)
return CGPoint(x: (pixelAddress / bytesPerpixel) % width, y: (pixelAddress / bytesPerpixel) / width)
}
}
}
return nil
}
func firstUniquePixel( in image: UIImage) -> CGPoint? {
return pixelNotMatchingTopLeftColor(in: image, first: true)
}
func lastUniquePixel( in image: UIImage) -> CGPoint? {
return pixelNotMatchingTopLeftColor(in: image, first: false)
}
你必须对左右做类似的事情(如果你在计算指针数学时遇到问题,请告诉我)。然后使用顶部、左侧、底部和右侧调用 CGImageCreateWithImageInRect.