【问题标题】:Make UIImageView image slightly smaller使 UIImageView 图像略小
【发布时间】:2015-07-03 02:38:00
【问题描述】:

我想让 UIImageView 中的图像比最初为我设置的图像略小。

我已将 contentMode 发送到 UIViewContentModeScaleAspectFill,但我仍想将其稍微小一些。

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

【问题讨论】:

  • 你为什么不尝试更新你的 imageView 框架的大小?
  • 试图更新我的框架的大小,但它不起作用

标签: ios objective-c uiimageview setbounds


【解决方案1】:

假设你从情节提要的出口是

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

在您的编码中使用它来制作 UIImageView 的“填充”,这应该会为您提供图像尺寸小于您的框架的结果。

imageView.bounds = CGRectInset(imageView.frame, 10.0f, 10.0f);

【讨论】:

    【解决方案2】:

    你可以创建实用方法:

    - (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;
    }
    

    并实施它:

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

    希望这会有所帮助。

    【讨论】:

    • 这个方法只会让图片变模糊,不会调整大小
    • 并非如此。 UIImage 没有缩放或绝对大小,因此除非您在 Core Graphics 中强制缩减采样或绘制算法,否则它总是会根据视图调整大小。
    猜你喜欢
    • 2019-04-04
    • 2018-01-16
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    相关资源
    最近更新 更多