마스크가 적용된 이미지를 렌더링하는 UIView 하위 클래스가 있습니다. 그것은 마스크가 완전히 투명하게 렌더링되는 와이드 컬러 노출 영역 디스플레이 (최신 iPad Pro)가있는 경우를 제외하고 모든 장치 (iPad 전용)에서 완벽하게 작동합니다 (보기와 같이 사용자에게는 보이지 않습니다). 관련 초기화 /의 drawRect 코드는 다음과 같습니다CGImage 마스크가 Wide Color Gamut 디스플레이에서 작동하지 않습니다.
이init(image: UIImage) {
scratchable = image.cgImage!
imageWidth = scratchable.width
imageHeight = scratchable.height
let colorspace = CGColorSpaceCreateDeviceGray()
let pixels = CFDataCreateMutable(nil, imageWidth * imageHeight)!
alphaPixels = CGContext(
data: CFDataGetMutableBytePtr(pixels),
width: imageWidth,
height: imageHeight,
bitsPerComponent: 8,
bytesPerRow: imageWidth,
space: colorspace,
bitmapInfo: CGImageAlphaInfo.none.rawValue
)!
provider = CGDataProvider(data: pixels)!
alphaPixels.setFillColor(UIColor.black.cgColor)
let mask = CGImage(
maskWidth: imageWidth,
height: imageHeight,
bitsPerComponent: 8,
bitsPerPixel: 8,
bytesPerRow: imageWidth,
provider: provider,
decode: nil,
shouldInterpolate: false
)!
scratched = scratchable.masking(mask)!
super.init(frame: CGRect(x: 0, y: 0, width: imageWidth/2, height: imageHeight/2))
alphaPixels.fill(imageRect)
isOpaque = false
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()!
context.saveGState()
context.translateBy(x: 0, y: bounds.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.draw(scratched, in: rect)
context.restoreGState()
}
(등 상황, 이유 pixels
, alphaPixels
의 경우, 필요한 마스크에 영향을 미치는 상황으로 그리는 수업 시간에 다른 코드로 인해)입니다.
왜 넓은 색 영역 표시가이 문제에 영향을 미치는지 또는 문제를 해결하기 위해 무엇을 할 수 있는지 생각해보십시오. 나는 그것이 색 공간과 관련이있을 것이라고 생각했지만, 문서는 정확히 마스크가 CGColorSpaceCreateDeviceGray
을 사용해야한다고 명시하고있다. http://d.pr/f/IS4SEF이
iPhoneX에서 문제입니까 (넓은 색상 영역도 있습니다)? 시뮬레이터에서 재현 할 수 있습니까? – paiv
올바른 결과 이미지와 나쁜 결과 이미지를 연결할 수 있습니까? – paiv
@paiv 이것은 iPad 전용 응용 프로그램이며 iPhone X도 없으므로 확실하지 않습니다. 나는 그것이 효과가 없을 것이라고 추정한다. 시뮬레이터에서는 재생할 수 없으며 장치에서만 재생할 수 있습니다. 방금 샘플 프로젝트 링크를 사용하여 질문을 편집했습니다. 어떤 도움을 주셔서 감사합니다! – pwightman