0
CoreGraphics 또는 CoreImage와 Swift3로 이미지를 자르거나 자르는 쉬운 방법이 있는지 궁금합니다.Swift3 - CoreGraphics 또는 CoreImage로 이미지의 지루한 부분을 잘라내거나 자르십시오.
상단에 입력 이미지 (빨간색/장미색 배경)가 있으면 그 빨간색 픽셀을 멀리 트리밍하여 실제 재미있는 비트 만 남겨 놓을 수 있습니까?
CoreGraphics 또는 CoreImage와 Swift3로 이미지를 자르거나 자르는 쉬운 방법이 있는지 궁금합니다.Swift3 - CoreGraphics 또는 CoreImage로 이미지의 지루한 부분을 잘라내거나 자르십시오.
상단에 입력 이미지 (빨간색/장미색 배경)가 있으면 그 빨간색 픽셀을 멀리 트리밍하여 실제 재미있는 비트 만 남겨 놓을 수 있습니까?
난 그냥 여기에 비슷한 질문의 일부를 대답 Iterate over individual pixels of a UIImage with height information
가 여기에 상단과 하단 독특한 픽셀을 얻는 방법입니다.
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이라고 부릅니다.