iOS开辟 image配景图片拉伸题目

计算机软件开发 2024-9-27 22:38:58 29 0 来自 中国
前言(假如是imageView的图片拉伸题目,可直接看文章末了,OC和Swift)

在开辟中谈天、按钮等配景图片,UI筹划师可以仅筹划其边框样式,然后通过代码就行处置惩罚,以顺应谈天笔墨的巨细或按钮的巨细。如许不光可以使安装包更轻巧而且可以更机动的利用图片;
方法一:

即将弃用方法
这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,必要两个参数,第一个是左边不拉伸地域的宽度,第二个参数是上面不拉伸的高度。
根据设置的宽度和高度,将接下来的一个像素举行左右扩展和上下拉伸。
- (UIImage *)stretchableImageWithLeftCapWidthNSInteger)leftCapWidth topCapHeightNSInteger)topCapHeight;注意:可拉伸的范围都是间隔leftCapWidth后的1竖排像素,和间隔topCapHeight后的1横排像素。
   UIImage *image = [UIImage imageNamed"圆角矩形-7-拷贝"];            //原始图片样式添加    self.originalImageView.image = image;        //没处置惩罚好的图片    self.badImageView.image = image;//图片处置惩罚后的 将被弃用 方法一://这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,必要两个参数,第一个是左边不拉伸地域的宽度,第二个参数是上面不拉伸的高度。//根据设置的宽度和高度,将接下来的一个像素举行左右扩展和上下拉伸。    [self.textImageView setImage:[image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5]];//注意:可拉伸的范围都是间隔leftCapWidth后的1竖排像素,和间隔topCapHeight后的1横排像素。图片讲授



可拉伸的范围都是间隔leftCapWidth后的1竖排像素,和间隔topCapHeight后的1横排像素。
方法二:

iOS 5 推出
- (UIImage *)resizableImageWithCapInsetsUIEdgeInsets)capInsets; // create a resizable version of this image. the interior is tiled when drawn.   UIImage *image = [UIImage imageNamed"圆角矩形-7-拷贝"];            //原始图片样式添加    self.originalImageView.image = image;        //没处置惩罚好的图片    self.badImageView.image = image;//处置惩罚图片 iOS 5 方法三:            CGFloat width = image.size.width;            CGFloat height = image.size.height;                        CGFloat top = height/2.0f - 0.5f; // 顶端盖高度            CGFloat bottom = height/2.0f - 0.5f ; // 底端盖高度            CGFloat left = width/2.0f - 0.5f; // 左端盖宽度            CGFloat right = width/2.0f - 0.5f; // 右端盖宽度            UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);                        //创建 一个可变的image版本,内部平铺,类:UIImageResizingModeTile模式;            self.textImageView.image = [image resizableImageWithCapInsets:insets];图片讲授



中心的框框是复制平铺地域,在本工程中为2px巨细,Cap部门(即线的地域)为保留样式
方法三:

iOS 6 方法
- (UIImage *)resizableImageWithCapInsetsUIEdgeInsets)capInsets resizingModeUIImageResizingMode)resizingMode; // the interior is resized according to the resizingMode   UIImage *image = [UIImage imageNamed"圆角矩形-7-拷贝"];            //原始图片样式添加    self.originalImageView.image = image;        //没处置惩罚好的图片    self.badImageView.image = image;            //处置惩罚图片 iOS 6 方法二:            CGFloat width = image.size.width;            CGFloat height = image.size.height;                        CGFloat top = height/2.0f - 0.5f; // 顶端盖高度            CGFloat bottom = height/2.0f - 0.5f ; // 底端盖高度            CGFloat left = width/2.0f - 0.5f; // 左端盖宽度            CGFloat right = width/2.0f - 0.5f; // 右端盖宽度            UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);            // 指定为拉伸模式,伸缩后重新赋值                        //UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形地域来添补图片            //UIImageResizingModeTile:平铺模式,通过重复表现UIEdgeInsets指定的矩形地域来添补图片            _textImageView.image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeTile];// Swiftlet image = UIImage(named: "rounded_rectangle_7_copy")if image != nil {      self.originalImageView.image = image      self.badImageView.image = image  } else {      self.originalImageView.image = image      self.badImageView.image = image  }let width = image?.size.width ?? 0  let height = image?.size.height ?? 0let top = height / 2 - 0.5  let bottom = height / 2 - 0.5  let left = width / 2 - 0.5  let right = width / 2 - 0.5let insets = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)if image != nil {      let resizingMode = UIImage.resizableImageResizingMode.tile      self.textImageView.image = UIImage(resizableImageWithCapInsets: insets, resizingMode: resizingMode)  } else {      self.textImageView.image = UIImage(resizableImageWithCapInsets: insets, resizingMode: resizingMode)  }增补知识

关于imageView的图片拉伸题目,在这里稍作总结,渴望可以给你提供资助。
typedef NS_ENUM(NSInteger, UIViewContentMode) {    UIViewContentModeScaleToFill,    UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent    UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.    UIViewContentModeTop,    UIViewContentModeBottom,    UIViewContentModeLeft,    UIViewContentModeRight,    UIViewContentModeTopLeft,    UIViewContentModeTopRight,    UIViewContentModeBottomLeft,    UIViewContentModeBottomRight,};//利用方法[ImageView setContentMode:UIViewContentModeScaleAspectFit];//ORImageView.contentMode = UIViewContentModeScaleAspectFit;//以下方法,图片保持原有巨细比例的情况下,展示在ImageView的上下左右等位置;假如视图巨细小于图片的尺寸,则图片会超出视图边界;    UIViewContentModeTop,    UIViewContentModeBottom,    UIViewContentModeLeft,    UIViewContentModeRight,    UIViewContentModeTopLeft,    UIViewContentModeTopRight,    UIViewContentModeBottomLeft,    UIViewContentModeBottomRight,    UIViewContentModeCenter,      UIViewContentModeScaleToFill,  //根据视图的比例去拉伸图片内容。图片大概变形;    UIViewContentModeScaleAspectFit,  //保持图片内容的纵横比例,来顺应视图的巨细。    UIViewContentModeScaleAspectFill,   //用图片内容来添补视图的巨细,多余得部门可以被修剪掉来添补整个视图边界。     UIViewContentModeRedraw,     //这个选项是单视图的尺寸位置发生厘革的时间通过调用setNeedsDisplay方法来重新表现。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:51, Processed in 0.123100 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表