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

前端 未结 3 1223
盖世英雄少女心
盖世英雄少女心 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:34

    You don't need to set the contentsRect (and if you do, it should be in the unit coordinate space, probably just CGRectMake(0, 0, 1.0, 1.0).

    You might want to set the layer's position property.

    0 讨论(0)
  • 2021-02-06 11:35

    You could load the image into a UIImageView, which is decended from UIView and therefore has it's own layer property.

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
    imgView.image = [UIImage imageNamed:@"mypic.png"];
    
    [[[self view] layer] addSublayer:[imgView layer]];
    [imgView release];
    
    0 讨论(0)
  • 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.

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