iPhone: How do I add layers to UIView's root layer to display an image with the content property?

前端 未结 3 1258
盖世英雄少女心
盖世英雄少女心 2021-02-06 11:03

According to the Mac Dev Center docs, you should be able to set the contents property of a CALayer and have that render automatically. However, I still can\'t get a simple image

3条回答
  •  长发绾君心
    2021-02-06 11:41

    You need to create two CALayer . This is perfect way to display the image within the CALayer.

    CALayer *pulseLayer_ = [[CALayer layer] retain];
    
    pulseLayer_.backgroundColor = [[UIColor whiteColor] CGColor];
    pulseLayer_.bounds = CGRectMake(0., 0., 80., 80.);
    pulseLayer_.cornerRadius = 12.;
    pulseLayer_.position = self.view.center;
    
    [self.view.layer addSublayer:pulseLayer_];
    
    
    CALayer *imageLayer = [CALayer layer];
    
    imageLayer.frame = pulseLayer_.bounds;
    imageLayer.cornerRadius = 10.0;
    imageLayer.contents = (id) [UIImage imageNamed:@"jacklogo.png"].CGImage;
    imageLayer.masksToBounds = YES;
    
    [pulseLayer_ addSublayer:imageLayer];
    
    [pulseLayer_ setNeedsDisplay];
    

    I think its make solution to your problem.

提交回复
热议问题