2016-12-08 4 views
0

J'essaie de créer des vignettes avec des coins arrondis et une barre en bas de l'image sur iOS.J'ai rencontré des dents de scie bizarres lorsque j'ai dessiné des images de coins arrondis sur iOS

et moi avons le code suivant:

extension UIImage { 
    func thumbnails(with options: SomeDrawingOptions) -> UIImage? { 
    // ... 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { 
     UIGraphicsEndImageContext() 
    } 
    let context = UIGraphicsGetCurrentContext()! 

    UIColor.white.setFill() 
    context.fill(targetRect) 

    UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip() 

    // Drawing image 
    draw(in: targetRect) 

    // Drawing a transparent mask 
    UIColor.black.withAlphaComponent(0.4).setFill() 
    context.fill(targetRect) 

    // Drawing bar 
    UIColor.white.setFill() 
    context.fill(someBarRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
    } 
} 

Enfin, j'ai une image avec des coins arrondis pointus, tout comme ceux de l'instantané suivant.

Sharp Rounded Corners

Tout conseil pour éliminer les dents de scie des coins arrondis?

Solution ======= ====== ajouté

Merci pour la réponse @ wj2061, le problème résolu. Et voici le code:

extension UIImage { 
    func thumbnails(with options: SomeDrawingOptions) -> UIImage? { 
    // ... 

    let targetRect = options.targetRect 
    let clippedPath = UIBezierPath(roundedRect: targetRect, cornerRadius: 8) 

    func makeImage() -> UIImage? { 
     UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
     defer { UIGraphicsEndImageContext() } 
     let context = UIGraphicsGetCurrentContext()! 

     draw(in: targetRect) 

     // Drawing a transparent mask 
     UIColor.black.withAlphaComponent(0.4).setFill() 
     context.fill(targetRect) 

     // Drawing bar 
     UIColor.white.setFill() 
     context.fill(someBarRect) 

     return UIGraphicsGetImageFromCurrentContext() 
    } 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { UIGraphicsEndImageContext() } 
    let context = UIGraphicsGetCurrentContext()! 

    // Drawing image 
    clippedPath.addClip() 
    makeImage()?.draw(in: targetRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
    } 
} 
+0

@ Matt les coins arrondis ne doit pas être vu lorsque la couleur de la barre de l'image est la même que la couleur d'arrière-plan de la vue conteneur (dans ce cas, il est le point de vue de la collecte) – mrahmiao

Répondre

1

Il semble donc que ce piège a quelque chose à voir avec UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip(), et je tente d'ajouter un masque à l'image à l'étape 1, ajouter coin à l'image à l'étape 2. Le code est comme ce

extension UIImage { 
func thumbnails() -> UIImage? { 
    let targetRect = CGRect(x: 0, y: 0, width: 100, height: 150) 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { 
     UIGraphicsEndImageContext() 
    } 

    UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip() 

    let makedImage = self.makedImage() 

    makedImage?.draw(in : targetRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
} 


func makedImage()->UIImage?{ 
    let targetRect = CGRect(x: 0, y: 0, width: 100, height: 150) 
    let someBarRect = CGRect(x: 0, y: 100, width: 100, height: 50) 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { 
     UIGraphicsEndImageContext() 
    } 
    let context = UIGraphicsGetCurrentContext()! 

    draw(in : targetRect) 


    // Drawing a transparent mask 
    UIColor.black.withAlphaComponent(0.4).setFill() 
    context.fill(targetRect) 

    // Drawing bar 
    UIColor.white.withAlphaComponent(1).setFill() 
    context.fill(someBarRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
} 
} 
+0

Après la restauration gSTATE() , la barre ne sera pas coupée, et les deux coins arrondis en bas ont disparu. – mrahmiao

+0

Il semble que 'UIGraphicsGetCurrentContext.clear()' est la méthode que vous recherchez. Je change aussi l'ordre ou 'context.fill (targetRect)', de sorte que l'image n'a pas de coin supérieur blanc. – wj2061

+0

Cela a effectivement fonctionné. La dent de scie a été éliminée. Cependant, une barre avec une couleur transparente ne peut pas être peinte. Bien que je mette une couleur transparente remplie, l'effet transparent ne peut pas être vu. – mrahmiao