UIImage as CAShapeLayer contents

后端 未结 5 1831
遥遥无期
遥遥无期 2021-01-14 17:23

If I create a CAShapeLayer with just a background color it shows up, but if i set the content to an image, nothing shows.

CAShapeLayer* leftDot = [CAShapeLa         


        
相关标签:
5条回答
  • 2021-01-14 17:33

    You can get the image to appear by setting the fillColor rather than the contents:

    layer.fillColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image"]].CGColor;
    
    0 讨论(0)
  • 2021-01-14 17:36

    CAShapeLayer doesn't work that way. You would create a normal CALayer with the image as its content and then use the shape layer as the mask of that layer to achieve the effect you are after.

    0 讨论(0)
  • 2021-01-14 17:37
    layerName.contents = (id)[[UIImage imageNamed:@"img.PNG"] CGImage];
    
    0 讨论(0)
  • 2021-01-14 17:55

    You have to set the CGImage for the layer

    leftDot.fillColor=[UIColor clearColor].CGColor ;
    [leftDot setContents:(__bridge id)[UIImage imageNamed: @"glow.png"].CGImage];
    
    0 讨论(0)
  • 2021-01-14 17:58

    [NOTE: updated to Swift 2.2 and Swift 3]

    Quick answer: use CALayer

    As explained in CAShapeLayer Class Reference, a CAShapeLayer is used for drawing shapes using a custom path. Setting an image as its contents will simply be ignored.

    If you just want to show an image in a layer (let's suppose your file is named image.png), you can use a CALayer and set its contents property so that it references the CGImage representation of your image:

    Swift 2.2:

    let layer = CALayer()
    layer.contents = UIImage(named: "image")?.CGImage
    

    Swift 3:

    let layer = CALayer()
    layer.contents = UIImage(named: "image")?.cgImage
    

    Longer answer: keep using CAShapeLayer

    Since the OP asked specifically about CAShapeLayer, the question may read like "How can I crop an image into a specific shape using CAShapeLayer?"

    There are two solutions I'd suggest you try:

    1) Masking

    Create a normal CALayer and set the image as its contents. Then create a CAShapeLayer with the path you like and use it as the first layer's mask.

    Swift 2.2:

    let imageLayer = CALayer()
    imageLayer.contents = UIImage(named: "image")?.CGImage // Assign your image
    imageLayer.frame = ... // Define a frame
    
    let maskPath = UIBezierPath(...) // Create your path
    let maskLayer = CAShapeLayer()
    maskLayer.path = maskPath.CGPath
    
    imageLayer.mask = maskLayer // Set the mask
    

    Swift 3:

    let imageLayer = CALayer()
    imageLayer.contents = UIImage(named: "image")?.cgImage // Assign your image
    imageLayer.frame = ... // Define a frame
    
    let maskPath = UIBezierPath(...) // Create your path
    let maskLayer = CAShapeLayer()
    maskLayer.path = maskPath.cgPath
    
    imageLayer.mask = maskLayer // Set the mask
    

    Don't forget to set the right frames and paths, and you should be able to achieve the effect you wanted.

    2) Fill color

    Create a CAShapeLayer with the path you like, then use your image as its fillColor.

    Swift 2.2:

    let path = UIBezierPath(...) // Create your path
    let layer = CAShapeLayer()
    layer.path = path.CGPath
    
    let image = UIImage(named: "image") // Assign your image
    layer.fillColor = UIColor(patternImage: image!).CGColor
    

    Swift 3:

    let path = UIBezierPath(...) // Create your path
    let layer = CAShapeLayer()
    layer.path = path.cgPath
    
    let image = UIImage(named: "image") // Assign your image
    layer.fillColor = UIColor(patternImage: image!).cgColor
    

    You may find this approach easier at first, but controlling the way the image fills your shape is not trivial at all.

    0 讨论(0)
提交回复
热议问题