2017-04-07 1 views
0

스타와 같은 이미지 패턴을 그리려는 경우, 그 색상을 변경할 수 있습니다. 이미지보기를 사용하지 않고 이미지 자체의 색상을 변경할 수 있습니까? 나는 당신의 이미지가있는 UIView에서와 같이, 그것은 오래하는 UIView의 한 속성입니다있는 UIImageView의 색조 색상을 변경 많은 답을 보았지만, 효과는 UIImageView의뿐만 아니라 속성입니다있는 UIImageswift3에서 Uiimageview를 사용하지 않고 UIImage 색상을 변경하십시오.

+0

당신이의 –

+1

가능한 중복 무엇을하려고 몇 가지 코드를 제공 할 수 검은 색에 있어야 자산 에 추가 [A있는 UIImage에하려면 tintColor을 적용하는 방법?] (http://stackoverflow.com/questions/19802982/tintcolor-to-a-ui 이미지) – inokey

+0

아래에 게시 된 솔루션을 찾았습니다 – Van

답변

0

해답을 찾아 주셔서 감사합니다. 그러나 해결책을 얻었습니다.

func changePatternImageColor() { 
    patternImage = UIImage(named: "pattern_star.png")! //make sure to reinitialize the original image here every time you change the color 
    UIGraphicsBeginImageContext(patternImage.size) 
    let context = UIGraphicsGetCurrentContext() 
    let color = UIColor(red: self.red, green: self.green, blue: self.blue, alpha: self.opacity) 
    color.setFill() 
    context?.translateBy(x: 0, y: patternImage.size.height) 
    context?.scaleBy(x: 1.0, y: -1.0) 
    //set the blend mode to color burn, and the original image 
    context?.setBlendMode(CGBlendMode.exclusion) //this does the magic. 
    let rect = CGRect(x: 0, y: 0, width: patternImage.size.height, height: patternImage.size.height) 
    context?.draw(patternImage.cgImage!, in: rect) 
    //set the mask that matches the shape of the image, then draw color burn a colored rectangle 
    context?.clip(to: rect, mask: patternImage.cgImage!) 
    context?.addRect(rect) 
    context?.drawPath(using: CGPathDrawingMode.fill) 
    patternImage = UIGraphicsGetImageFromCurrentImageContext()! 
    image_ref = patternImage.cgImage 
    UIGraphicsEndImageContext() 
} 

patternImage는 자산에 추가 된 이미지입니다. 오후 8시 30 분 P.S. patternImage는

0

tintColor에 적용되지 않습니다 UIView.tintColor을 사용할 수 있습니다.

+0

컨텍스트에서 패턴을 그릴 것이므로 이미지 뷰가 아닌 이미지 뷰 – Van

관련 문제