1

I would like to make my image in UIImageView slightly smaller than it originally set for me.

I had send the contentMode to UIViewContentModeScaleAspectFill but I still want to make it slightly smaller.

self.imgCamera.layer.cornerRadius = self.imgCamera.frame.size.width / 2;
self.imgCamera.contentMode=UIViewContentModeScaleAspectFill;
self.imgCamera.clipsToBounds = YES;

2 Answers2

1

Let's say your outlet from storyboard is

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

Inside your coding use this to make the "padding" of your UIImageView, this should give you the result of the image size smaller than your frame.

imageView.bounds = CGRectInset(imageView.frame, 10.0f, 10.0f);
Jen Shen
  • 11
  • 2
0

You could create utility method:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

and implement it:

UIImage *myIcon = [self imageWithImage:myImage scaledToSize:CGSizeMake(20, 20)]

Hope this could help.

Nghia Luong
  • 790
  • 1
  • 6
  • 11