iOS图片的处理

    xiaoxiao2026-03-30  11

    UIImageView图片填充模式:

    规律: Aspect代表图片的宽高比来拉伸.Fill 代表填充满 Fit代表不超出填充模式: 1.UIViewContentModeScaleToFill 2.UIViewContentModeScaleAspectFit 3.UIViewContentModeScaleAspectFill

    iOS-UIView-设置背景图片4种方式

    1.将图片作为UIView的背景色

    如果图片大小不够,就会平铺多张图片,不会去拉伸图片以适应View的大小,过于占内存,不建议使用。
    方法1.
    imageNamed方式,在View释放后,color不会跟着释放,而是一直存在内存中; self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.jpg"]];
    方法2.
    color会跟着释放掉,当然再次生成color时就会再次申请内存。 NSString *path = [[NSBundle mainBundle] pathForResource:@"image"ofType:@"jpg"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; UIColor *color = [UIColor colorWithPatternImage:image]; self.view.backgroundColor = color;

    2.添加一个UIImageView显示图片

    方法 1.
    在UIView上再添加一个UIImageView显示图片作为UIView的背景图片

    注意:如果有点击事件的话, userInteractionEnabled用户交互设置为YES。

    3.添加一个图层

    方法 1.(推荐)
    NSString *path = [[NSBundle mainBundle]pathForResource:@"image"ofType:@"jpg"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; self.view.layer.contents = (id)image.CGImage;

    注意: 要写清楚后缀,即使是”.png”。

    图片拉伸

    - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode typedef struct UIEdgeInsets { CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset' } UIEdgeInsets; typedef NS_ENUM(NSInteger, UIImageResizingMode) { UIImageResizingModeTile, // 平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片 UIImageResizingModeStretch, // 拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片 };

    具体代码

    // 加载图片 UIImage *image = [UIImage imageNamed:@"chat_send_nor"]; // 设置端盖的值 CGFloat top = image.size.height * 0.5; CGFloat left = image.size.width * 0.5; CGFloat bottom = image.size.height * 0.5; CGFloat right = image.size.width * 0.5; // 设置端盖的值 UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right); // 设置拉伸的模式 UIImageResizingMode mode = UIImageResizingModeStretch; // 拉伸图片 UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode]; // 设置按钮的背景图片 [btn setBackgroundImage:newImage forState:UIControlStateNormal];

    iOS中拉伸图片的几种方式 UI-UIImageView的图片填充方式(contentMode)_图片作为控件背景图的拉伸方式(stretch)介绍

    转载请注明原文地址: https://ju.6miu.com/read-1308330.html
    最新回复(0)