Tiling a UIImageView using resizeableImageWithCapI

2019-06-06 21:29发布

问题:

I'm using the following code to try to create a tiled image with cap insets, and seeing the image below as the results in the simulator. My source image is 145x83 pixels in size, and shown here too. What am I doing wrong?

SOURCE IMAGE:

RESULTS IN SIMULATOR:

CODE:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *cloudImage = [UIImage imageNamed: @"cloud.png"];
    [cloudImage resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 10, 10) resizingMode: UIImageResizingModeTile];

    UIImageView *imageView1 = [[UIImageView alloc] initWithImage: cloudImage];
    [imageView1 setFrame: CGRectMake(50, 50, 100, 100)];

    UIImageView *imageView2 = [[UIImageView alloc] initWithImage: cloudImage];
    [imageView2 setFrame: CGRectMake(250, 50, 200, 200)];

    [self.view addSubview: imageView1];
    [self.view addSubview: imageView2];

}

回答1:

You are not assigning the resizable image to anything, you need to either do:

UIImage *cloudImage = [[UIImage imageNamed: @"cloud.png"] resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 10, 10) resizingMode: UIImageResizingModeTile];

or

cloudImage = [cloudImage resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 10, 10) resizingMode: UIImageResizingModeTile];