Eric Brotto
Eric Brotto

Reputation: 54251

Using UIImageView's layer.border property to set the border

I have an UIImageView which has an image. The initWithFrame:(CGRect)frame looks like this:

#define BORDER_WIDTH_SELECTION 3
#define SELECTED_SCALE_VALUE 1.3

    self.layer.masksToBounds = NO;
    smallFrame_ = frame;
    float newX = self.center.x - ((self.frame.size.width*SELECTED_SCALE_VALUE)*0.5);
    float newY = self.center.y - ((self.frame.size.height*SELECTED_SCALE_VALUE)*0.5);
    bigFrame_ = CGRectMake(newX, newY, self.frame.size.width*SELECTED_SCALE_VALUE, self.frame.size.height*SELECTED_SCALE_VALUE);
    self.layer.borderWidth = BORDER_WIDTH_SELECTION;

then later I trigger this

- (void) setSelected {
    [self makeThumbnailBig];
    self.layer.borderColor = [UIColor colorWithRed:178.0/255.0 green:19.0/255.0 blue:50.0/255.0 alpha:1.0].CGColor;
}

- (void)makeThumbnailBig
{    
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = bigFrame_;
    }];
}

The ImageView scales correctly and draws a red border, but then the image within the ImageView peaks out from underneath the border on the left by about a pixel or two. I have removed the image, but this should give you an idea of the issue:

enter image description here

Any ideas?

Upvotes: 1

Views: 412

Answers (1)

voromax
voromax

Reputation: 3389

In your code there is a line

self.layer.masksToBounds = NO;

According to the question it should be YES instead

About extra pixels:

When you are preparing the image size by division you could face the issue of magic image high resolution optimization. It starts by adding the fractal part to the UIImageView dimension. See Points Versus Pixels

Upvotes: 4

Related Questions